/* =========================================================================
   Tekno Motion Control - Admin / Auth pages (responsive + 60fps)
   Mobile-first, fluid typography via clamp(), hardware-accelerated motion.
   ========================================================================= */

*,
*::before,
*::after {
  margin: 0;
  padding: 0;
  box-sizing: border-box;
}

:root {
  --primary: #6c5ce7;
  --primary-dark: #5a4bd1;
  --success: #00b894;
  --warning: #fdcb6e;
  --danger: #e17055;
  --info: #74b9ff;
  --bg: #0f0f23;
  --bg-card: #1a1a2e;
  --bg-input: #16213e;
  --text: #f0f0f0;
  --text-muted: #a0a0b0;
  --border: #2d2d44;
  --border-color: #2d2d44;
  --card-bg: #1a1a2e;
  --radius: clamp(8px, 1.4vw, 14px);
  --gutter: clamp(0.75rem, 2.5vw, 1.5rem);
  --container: min(100% - 2 * var(--gutter), 980px);

  --fs-xs: clamp(0.7rem, 0.65rem + 0.2vw, 0.8rem);
  --fs-sm: clamp(0.8rem, 0.75rem + 0.25vw, 0.9rem);
  --fs-md: clamp(0.9rem, 0.85rem + 0.3vw, 1rem);
  --fs-lg: clamp(1.05rem, 0.95rem + 0.5vw, 1.25rem);
  --fs-xl: clamp(1.4rem, 1.1rem + 1.5vw, 2.1rem);

  color-scheme: dark;
}

@media (prefers-reduced-motion: reduce) {
  *,
  *::before,
  *::after {
    animation-duration: 0.01ms !important;
    animation-iteration-count: 1 !important;
    transition-duration: 0.01ms !important;
    scroll-behavior: auto !important;
  }
}

html {
  -webkit-text-size-adjust: 100%;
  text-size-adjust: 100%;
  scroll-behavior: smooth;
  min-height: 100%;
  min-height: 100dvh;
}

body {
  font-family: "Segoe UI", system-ui, -apple-system, "Helvetica Neue", Arial, sans-serif;
  background: var(--bg);
  color: var(--text);
  line-height: 1.55;
  min-height: 100vh;
  min-height: 100dvh;
  -webkit-font-smoothing: antialiased;
  -moz-osx-font-smoothing: grayscale;
  text-rendering: optimizeLegibility;
  overflow-x: hidden;
  padding: env(safe-area-inset-top) env(safe-area-inset-right)
    env(safe-area-inset-bottom) env(safe-area-inset-left);
}

img,
video,
canvas,
svg {
  display: block;
  max-width: 100%;
  height: auto;
}

button,
input,
select,
textarea {
  font: inherit;
  color: inherit;
}

button {
  cursor: pointer;
  background: none;
  border: 0;
  -webkit-tap-highlight-color: transparent;
  touch-action: manipulation;
}

a {
  -webkit-tap-highlight-color: transparent;
  color: var(--primary);
}

/* ========== Layout ========== */
.container {
  width: var(--container);
  margin-inline: auto;
  padding-block: clamp(1rem, 3vw, 2rem);
}

/* ========== Header ========== */
header {
  text-align: center;
  margin-bottom: clamp(1.5rem, 4vw, 2.5rem);
  position: relative;
}

header h1 {
  font-size: var(--fs-xl);
  background: linear-gradient(135deg, var(--primary), #a29bfe);
  -webkit-background-clip: text;
  -webkit-text-fill-color: transparent;
  background-clip: text;
  margin-bottom: 0.5rem;
  display: inline-flex;
  align-items: center;
  gap: 0.6rem;
  flex-wrap: wrap;
  justify-content: center;
  line-height: 1.15;
}

.subtitle {
  color: var(--text-muted);
  font-size: var(--fs-md);
}

.back-link {
  display: inline-flex;
  align-items: center;
  margin-top: 0.6rem;
  color: var(--primary);
  text-decoration: none;
  font-size: var(--fs-sm);
  padding: 0.4rem 0.8rem;
  border-radius: 8px;
  min-height: 36px;
  transition: opacity 0.2s ease, background 0.2s ease;
}

.back-link:hover,
.back-link:focus-visible {
  opacity: 0.85;
  background: rgba(108, 92, 231, 0.1);
}

/* ========== Stats grid ========== */
.stats-grid {
  display: grid;
  grid-template-columns: repeat(2, 1fr);
  gap: 0.75rem;
  margin-top: 1rem;
}

@media (min-width: 720px) {
  .stats-grid {
    grid-template-columns: repeat(4, 1fr);
    gap: 1rem;
  }
}

.stat-card {
  background: var(--bg-card);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  padding: clamp(0.85rem, 2vw, 1.25rem);
  text-align: center;
}

.stat-value {
  display: block;
  font-size: clamp(1.4rem, 1rem + 1.5vw, 1.8rem);
  font-weight: 700;
}

.stat-label {
  display: block;
  font-size: var(--fs-xs);
  color: var(--text-muted);
  margin-top: 0.25rem;
}

.stat-active .stat-value {
  color: var(--success);
}
.stat-disabled .stat-value {
  color: var(--danger);
}
.stat-current .stat-value {
  color: var(--info);
  font-size: clamp(1.1rem, 0.9rem + 1vw, 1.45rem);
}

/* ========== Sections ========== */
section {
  margin-bottom: clamp(1.25rem, 3vw, 2rem);
}

section h2 {
  font-size: var(--fs-lg);
  margin-bottom: 1rem;
}

/* ========== Settings card ========== */
.settings-card,
.add-key-card {
  background: var(--bg-card);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  padding: clamp(1rem, 2.5vw, 1.5rem);
}

.setting-row {
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 0.75rem 0;
  border-bottom: 1px solid var(--border);
  gap: 1rem;
  flex-wrap: wrap;
}

.setting-row:last-of-type {
  border-bottom: none;
  margin-bottom: 1rem;
}

.setting-info {
  flex: 1 1 220px;
  min-width: 0;
}

.setting-info label {
  font-weight: 600;
  font-size: var(--fs-sm);
  display: block;
}

.setting-info small {
  color: var(--text-muted);
  font-size: var(--fs-xs);
}

.setting-input {
  width: 140px;
  max-width: 100%;
  padding: 0.6rem 0.75rem;
  background: var(--bg-input);
  border: 1px solid var(--border);
  border-radius: 8px;
  color: var(--text);
  font-size: var(--fs-sm);
  min-height: 40px;
}

.setting-input:focus {
  outline: none;
  border-color: var(--primary);
  box-shadow: 0 0 0 3px rgba(108, 92, 231, 0.18);
}

/* ========== Toggle switch ========== */
.toggle-switch {
  position: relative;
  flex-shrink: 0;
}

.toggle-switch input {
  display: none;
}

.toggle-label {
  display: block;
  width: 48px;
  height: 26px;
  background: var(--border);
  border-radius: 999px;
  cursor: pointer;
  position: relative;
  transition: background 0.3s ease;
}

.toggle-label::after {
  content: "";
  position: absolute;
  top: 3px;
  left: 3px;
  width: 20px;
  height: 20px;
  background: white;
  border-radius: 50%;
  transition: transform 0.3s ease;
  will-change: transform;
}

.toggle-switch input:checked + .toggle-label {
  background: var(--success);
}

.toggle-switch input:checked + .toggle-label::after {
  transform: translate3d(22px, 0, 0);
}

/* ========== Form / inputs ========== */
.form-group {
  margin-bottom: 1rem;
}

.form-group label {
  display: block;
  font-weight: 600;
  margin-bottom: 0.4rem;
  font-size: var(--fs-sm);
}

.required {
  color: var(--danger);
}

input,
textarea,
select {
  width: 100%;
  padding: 0.7rem 0.9rem;
  background: var(--bg-input);
  border: 1px solid var(--border);
  border-radius: 8px;
  color: var(--text);
  /* avoid iOS auto-zoom */
  font-size: max(16px, var(--fs-md));
  transition: border-color 0.2s ease, box-shadow 0.2s ease;
  min-height: 44px;
}

input:focus,
textarea:focus,
select:focus {
  outline: none;
  border-color: var(--primary);
  box-shadow: 0 0 0 3px rgba(108, 92, 231, 0.18);
}

textarea {
  min-height: 120px;
  resize: vertical;
  font-family: ui-monospace, "Courier New", monospace;
  line-height: 1.5;
}

.key-input-wrapper {
  position: relative;
  display: flex;
  gap: 0.5rem;
}

.key-input-wrapper input {
  flex: 1;
  font-family: ui-monospace, "Courier New", monospace;
}

.btn-toggle-visibility {
  padding: 0 0.85rem;
  min-width: 44px;
  min-height: 44px;
  background: var(--bg-input);
  border: 1px solid var(--border);
  border-radius: 8px;
  color: var(--text);
  font-size: var(--fs-md);
  transition: background 0.2s ease;
}

.btn-toggle-visibility:hover,
.btn-toggle-visibility:focus-visible {
  background: var(--border);
}

.divider-text {
  text-align: center;
  color: var(--text-muted);
  font-size: var(--fs-sm);
  margin-bottom: 0.75rem;
  position: relative;
}

.divider-text::before,
.divider-text::after {
  content: "";
  position: absolute;
  top: 50%;
  width: 28%;
  height: 1px;
  background: var(--border);
}

.divider-text::before {
  left: 0;
}
.divider-text::after {
  right: 0;
}

/* ========== Buttons ========== */
.btn-primary {
  width: 100%;
  padding: 0.85rem 1rem;
  min-height: 48px;
  background: linear-gradient(135deg, var(--primary), var(--primary-dark));
  color: white;
  border: 0;
  border-radius: 10px;
  font-size: var(--fs-md);
  font-weight: 600;
  transition: transform 0.15s ease, opacity 0.2s ease, box-shadow 0.2s ease;
  will-change: transform;
}

.btn-primary:hover,
.btn-primary:focus-visible {
  transform: translate3d(0, -1px, 0);
  opacity: 0.96;
  box-shadow: 0 8px 24px rgba(108, 92, 231, 0.25);
}

.btn-primary:active {
  transform: translate3d(0, 0, 0);
}

.btn-secondary {
  padding: 0.55rem 1rem;
  min-height: 40px;
  background: var(--bg-input);
  color: var(--text);
  border: 1px solid var(--border);
  border-radius: 8px;
  font-size: var(--fs-sm);
  font-weight: 500;
  transition: background 0.2s ease;
  display: inline-flex;
  align-items: center;
  justify-content: center;
}

.btn-secondary:hover,
.btn-secondary:focus-visible {
  background: var(--border);
}

.btn-sm {
  padding: 0.45rem 0.85rem;
  min-height: 36px;
  font-size: var(--fs-xs);
}

.btn-danger {
  border-color: rgba(225, 112, 85, 0.3);
  color: var(--danger);
}

.btn-danger:hover,
.btn-danger:focus-visible {
  background: rgba(225, 112, 85, 0.1);
}

/* ========== Bulk add area ========== */
.bulk-add textarea {
  font-size: var(--fs-sm);
}

/* ========== Keys / Members toolbar ========== */
.keys-toolbar {
  display: flex;
  gap: 0.5rem;
  margin-bottom: 1rem;
  flex-wrap: wrap;
}

.keys-toolbar .btn-secondary {
  flex: 1 1 auto;
  min-width: 130px;
}

@media (min-width: 720px) {
  .keys-toolbar .btn-secondary {
    flex: 0 0 auto;
  }
}

/* ========== Keys/Members list ========== */
.keys-list {
  display: flex;
  flex-direction: column;
  gap: 0.75rem;
}

.key-card {
  background: var(--bg-card);
  border: 1px solid var(--border);
  border-radius: 12px;
  padding: 0.85rem 1rem;
  display: flex;
  align-items: center;
  gap: 0.85rem;
  flex-wrap: wrap;
  transition: border-color 0.2s ease, transform 0.2s ease;
  will-change: transform;
}

.key-card:hover {
  border-color: rgba(108, 92, 231, 0.4);
  transform: translate3d(0, -1px, 0);
}

.key-card.active {
  border-color: rgba(0, 184, 148, 0.4);
}

.key-card.disabled {
  border-color: rgba(225, 112, 85, 0.3);
  opacity: 0.78;
}

.key-card.current {
  border-color: var(--primary);
  box-shadow: 0 0 0 1px var(--primary);
}

.key-index {
  width: 36px;
  height: 36px;
  border-radius: 50%;
  background: var(--bg-input);
  display: flex;
  align-items: center;
  justify-content: center;
  font-weight: 700;
  font-size: var(--fs-sm);
  flex-shrink: 0;
}

.key-card.current .key-index {
  background: var(--primary);
  color: white;
}

.key-info {
  flex: 1 1 200px;
  min-width: 0;
}

.key-name {
  font-size: var(--fs-sm);
  font-weight: 600;
  margin-bottom: 0.15rem;
  word-break: break-word;
}

.key-value {
  font-family: ui-monospace, "Courier New", monospace;
  font-size: var(--fs-xs);
  color: var(--text-muted);
  word-break: break-all;
}

.key-meta {
  font-size: var(--fs-xs);
  color: var(--text-muted);
  margin-top: 0.2rem;
  word-break: break-word;
}

.key-status {
  padding: 0.2rem 0.6rem;
  border-radius: 6px;
  font-size: var(--fs-xs);
  font-weight: 600;
  text-transform: uppercase;
  white-space: nowrap;
  flex-shrink: 0;
}

.key-status.active {
  background: rgba(0, 184, 148, 0.15);
  color: var(--success);
}

.key-status.disabled {
  background: rgba(225, 112, 85, 0.15);
  color: var(--danger);
}

.key-status.testing {
  background: rgba(116, 185, 255, 0.15);
  color: var(--info);
}

.key-actions {
  display: flex;
  gap: 0.4rem;
  flex-shrink: 0;
  margin-left: auto;
  flex-wrap: wrap;
}

@media (max-width: 600px) {
  .key-actions {
    width: 100%;
    justify-content: flex-end;
    margin-top: 0.5rem;
    margin-left: 0;
  }
}

.key-actions button {
  min-width: 36px;
  min-height: 36px;
  padding: 0 0.45rem;
  border-radius: 8px;
  border: 1px solid var(--border);
  background: var(--bg-input);
  color: var(--text);
  font-size: var(--fs-sm);
  display: inline-flex;
  align-items: center;
  justify-content: center;
  transition: background 0.2s ease;
}

.key-actions button:hover,
.key-actions button:focus-visible {
  background: var(--border);
}

.key-actions button.btn-delete:hover,
.key-actions button.btn-delete:focus-visible {
  background: rgba(225, 112, 85, 0.2);
  border-color: var(--danger);
}

/* ========== Activity log ========== */
.log-toolbar {
  margin-bottom: 0.75rem;
}

.activity-log {
  background: var(--bg-card);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  padding: 1rem;
  max-height: clamp(220px, 40vh, 360px);
  overflow-y: auto;
  font-family: ui-monospace, "Courier New", monospace;
  font-size: var(--fs-xs);
  -webkit-overflow-scrolling: touch;
}

.log-entry {
  padding: 0.4rem 0;
  border-bottom: 1px solid rgba(45, 45, 68, 0.5);
  display: flex;
  gap: 0.6rem;
  flex-wrap: wrap;
}

.log-entry:last-child {
  border-bottom: none;
}

.log-time {
  color: var(--text-muted);
  white-space: nowrap;
  font-size: var(--fs-xs);
}

.log-message {
  flex: 1 1 200px;
  min-width: 0;
  word-break: break-word;
}

.log-message.success { color: var(--success); }
.log-message.error { color: var(--danger); }
.log-message.warning { color: var(--warning); }
.log-message.info { color: var(--info); }

.empty-state {
  text-align: center;
  color: var(--text-muted);
  padding: clamp(1.25rem, 4vw, 2rem);
  font-style: italic;
  font-size: var(--fs-sm);
}

/* ========== Footer ========== */
footer {
  text-align: center;
  padding: 1.5rem 0 calc(1rem + env(safe-area-inset-bottom));
  border-top: 1px solid var(--border);
  color: var(--text-muted);
  font-size: var(--fs-xs);
}

/* ========== Toast ========== */
.toast {
  position: fixed;
  bottom: calc(1.25rem + env(safe-area-inset-bottom));
  right: clamp(0.75rem, 3vw, 2rem);
  left: clamp(0.75rem, 3vw, auto);
  max-width: min(380px, calc(100vw - 1.5rem));
  padding: 0.8rem 1.1rem;
  border-radius: 10px;
  font-size: var(--fs-sm);
  font-weight: 500;
  z-index: 1000;
  animation: slideIn 0.3s ease;
  box-shadow: 0 12px 28px rgba(0, 0, 0, 0.35);
  will-change: transform, opacity;
}

@media (min-width: 600px) {
  .toast {
    left: auto;
  }
}

.toast.success { background: rgba(0, 184, 148, 0.95); color: white; }
.toast.error { background: rgba(225, 112, 85, 0.95); color: white; }
.toast.info { background: rgba(108, 92, 231, 0.95); color: white; }

@keyframes slideIn {
  from {
    transform: translate3d(0, 16px, 0);
    opacity: 0;
  }
  to {
    transform: translate3d(0, 0, 0);
    opacity: 1;
  }
}

/* ========== Brand logo ========== */
.brand-logo {
  width: clamp(40px, 8vw, 60px);
  height: clamp(40px, 8vw, 60px);
  border-radius: 12px;
  object-fit: cover;
  vertical-align: middle;
  box-shadow: 0 4px 14px rgba(99, 102, 241, 0.35);
  flex-shrink: 0;
}

/* ========== Wider screens ========== */
@media (min-width: 1280px) {
  :root {
    --container: min(100% - 4rem, 1080px);
  }
}

@media (min-width: 1600px) {
  :root {
    --container: min(100% - 6rem, 1200px);
  }
}

/* ========== Print fallback ========== */
@media print {
  .btn-primary,
  .btn-secondary,
  .key-actions,
  .keys-toolbar,
  .toast {
    display: none !important;
  }
}
