/* ============================================================
   api.rkul.tech · admin SPA — terminal-like, sobre, mono
   ============================================================ */

:root, :root[data-theme="dark"] {
  --bg:        #0b0d10;
  --bg-2:      #14171c;
  --bg-3:      #1c2027;
  --line:      #2a2f38;
  --ink:       #d8dde4;
  --ink-soft:  #8d96a4;
  --ink-hint:  #5a6171;
  --accent:    #ff3b3b;
  --accent-d:  #c70a0a;
  --ok:        #5fbf6c;
  --warn:      #f4b740;

  --grid:      rgba(255, 255, 255, 0.012);
  --audio-filter: invert(0.85) hue-rotate(180deg);

  --mono: ui-monospace, "JetBrains Mono", "Cascadia Mono", "SF Mono", Consolas, "DejaVu Sans Mono", monospace;
}

:root[data-theme="light"] {
  --bg:        #fafaf9;
  --bg-2:      #f1f2f4;
  --bg-3:      #e5e7ea;
  --line:      #c9cdd3;
  --ink:       #1a1d22;
  --ink-soft:  #4d5563;
  --ink-hint:  #8a93a1;
  --accent:    #d12727;
  --accent-d:  #9b1818;
  --ok:        #2d8a3d;
  --warn:      #b87a00;

  --grid:      rgba(0, 0, 0, 0.028);
  --audio-filter: none;
}

html, body { transition: background-color 0.2s ease, color 0.2s ease; }

* { box-sizing: border-box; margin: 0; padding: 0; }
[hidden] { display: none !important; }

html, body {
  background: var(--bg);
  color: var(--ink);
  font-family: var(--mono);
  font-size: 14px;
  min-height: 100vh;
  -webkit-font-smoothing: antialiased;
}

/* Bandeau de fond : grille très discrète (couleur via --grid, change selon thème) */
body::before {
  content: "";
  position: fixed; inset: 0;
  background-image:
    linear-gradient(to right, var(--grid) 1px, transparent 1px),
    linear-gradient(to bottom, var(--grid) 1px, transparent 1px);
  background-size: 28px 28px;
  pointer-events: none;
  z-index: 0;
}

/* ---- Header --------------------------------------------------- */
header.top {
  position: relative;
  z-index: 2;
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 14px 2.5%;
  border-bottom: 1px solid var(--line);
  background: var(--bg-2);
}
.brand {
  font-size: 13px;
  letter-spacing: 0.04em;
  color: var(--ink);
  display: flex;
  align-items: center;
  gap: 8px;
}
.brand .sub { color: var(--ink-soft); }
.brand .dot {
  display: inline-block;
  width: 8px; height: 8px;
  border-radius: 50%;
  background: var(--accent);
  box-shadow: 0 0 8px var(--accent);
}
.top-actions {
  display: flex;
  gap: 8px;
  align-items: center;
}

/* ---- Layout --------------------------------------------------- */
main {
  position: relative;
  z-index: 1;
  width: 95%;
  margin: 0 auto;
  padding: 4vh 0 6vh;
}

/* App shell (sidebar + content), affichée après login. */
.app {
  display: flex;
  gap: 28px;
  align-items: flex-start;
}
.sidebar {
  flex: 0 0 160px;
  position: sticky;
  top: 4vh;
  border-right: 1px solid var(--line);
  padding-right: 16px;
}
.nav-vertical {
  display: flex;
  flex-direction: column;
  gap: 2px;
}
.nav-item {
  font-family: var(--mono);
  font-size: 12px;
  letter-spacing: 0.18em;
  text-transform: uppercase;
  text-align: left;
  padding: 10px 12px;
  background: transparent;
  border: 1px solid transparent;
  border-left: 2px solid transparent;
  color: var(--ink-hint);
  cursor: pointer;
  transition: color 0.12s ease, background 0.12s ease, border-color 0.12s ease;
}
.nav-item:hover {
  color: var(--ink);
  background: var(--bg-2);
}
.nav-item.active {
  color: var(--accent);
  border-left-color: var(--accent);
  background: var(--bg-2);
}
.content {
  flex: 1;
  min-width: 0; /* indispensable pour que le contenu ne déborde pas en flex */
}

.view h2 {
  font-size: 12px;
  font-weight: 600;
  text-transform: uppercase;
  letter-spacing: 0.32em;
  color: var(--ink-soft);
  margin-bottom: 14px;
}
.view h2::before {
  content: "› ";
  color: var(--accent);
}

.bar {
  display: flex;
  align-items: center;
  justify-content: space-between;
  margin-bottom: 16px;
}

/* ---- Card / Form --------------------------------------------- */
.card {
  background: var(--bg-2);
  border: 1px solid var(--line);
  padding: 24px;
  width: 35%;
  min-width: 280px;
  display: flex;
  flex-direction: column;
  gap: 14px;
}
label {
  display: flex;
  flex-direction: column;
  gap: 6px;
  font-size: 11px;
  letter-spacing: 0.18em;
  text-transform: uppercase;
  color: var(--ink-soft);
}
input, select {
  background: var(--bg);
  border: 1px solid var(--line);
  color: var(--ink);
  font-family: var(--mono);
  font-size: 14px;
  padding: 10px 12px;
  outline: none;
  transition: border-color 0.15s ease, box-shadow 0.15s ease;
}
input:focus, select:focus {
  border-color: var(--accent);
  box-shadow: 0 0 0 2px rgba(255, 59, 59, 0.18);
}

/* ---- Buttons --------------------------------------------------- */
.btn-primary, .btn-ghost {
  font-family: var(--mono);
  font-size: 12px;
  letter-spacing: 0.18em;
  text-transform: uppercase;
  font-weight: 600;
  padding: 9px 14px;
  border: 1px solid currentColor;
  background: transparent;
  cursor: pointer;
  transition: background 0.12s ease, color 0.12s ease;
}
.btn-primary {
  color: var(--accent);
}
.btn-primary:hover { background: var(--accent); color: white; border-color: var(--accent); }
.btn-primary:disabled { opacity: 0.5; cursor: not-allowed; }
.btn-ghost { color: var(--ink-soft); }
.btn-ghost:hover { color: var(--ink); border-color: var(--ink); }

/* ---- Table ---------------------------------------------------- */
#keys-table {
  width: 100%;
  border-collapse: collapse;
  background: var(--bg-2);
  border: 1px solid var(--line);
  font-size: 13px;
}
#keys-table th, #keys-table td {
  padding: 10px 14px;
  text-align: left;
  border-bottom: 1px solid var(--line);
  vertical-align: middle;
}
#keys-table th {
  font-size: 10px;
  letter-spacing: 0.22em;
  text-transform: uppercase;
  color: var(--ink-hint);
  font-weight: 600;
  background: var(--bg-3);
}
#keys-table tbody tr:hover { background: var(--bg-3); }
#keys-table .cell-active { text-align: center; width: 80px; }
#keys-table .scope-badge {
  display: inline-block;
  padding: 2px 8px;
  border: 1px solid currentColor;
  font-size: 10px;
  letter-spacing: 0.18em;
  text-transform: uppercase;
}
.scope-transcription { color: #6da9ff; }
.scope-speech        { color: #c389ff; }
.scope-agent         { color: #ffb547; }
.scope-cellier       { color: var(--accent); }
.scope-deepseek      { color: #62d4c1; }
.scope-adresses      { color: #8ee06b; }

#keys-table .meta {
  font-size: 11px;
  color: var(--ink-hint);
  letter-spacing: 0.04em;
}
#keys-table .prefix {
  color: var(--ink-soft);
  font-weight: 600;
}
.row-actions {
  display: flex;
  gap: 6px;
  justify-content: flex-end;
}
.row-actions button {
  font-family: var(--mono);
  font-size: 10px;
  letter-spacing: 0.16em;
  text-transform: uppercase;
  padding: 5px 8px;
  background: transparent;
  border: 1px solid var(--line);
  color: var(--ink-soft);
  cursor: pointer;
  transition: all 0.12s ease;
}
.row-actions button:hover { color: var(--ink); border-color: var(--ink); }
.row-actions button.danger:hover { color: var(--accent); border-color: var(--accent); }

/* Select inline (permission) */
select.perm-select {
  background: var(--bg);
  border: 1px solid var(--line);
  color: var(--ink);
  font-family: var(--mono);
  font-size: 11px;
  letter-spacing: 0.1em;
  padding: 3px 6px;
  text-transform: uppercase;
}
select.perm-select:focus {
  outline: none;
  border-color: var(--accent);
}

/* Switch active inline */
.toggle {
  appearance: none;
  -webkit-appearance: none;
  width: 34px; height: 18px;
  border-radius: 999px;
  background: var(--bg-3);
  border: 1px solid var(--line);
  position: relative;
  cursor: pointer;
  display: inline-block;
  vertical-align: middle;
  flex-shrink: 0;
  margin: 0;
  padding: 0;
  box-sizing: border-box;
  transition: background 0.15s ease, border-color 0.15s ease;
}
.toggle::after {
  content: "";
  position: absolute;
  top: 50%;
  left: 2px;
  width: 12px; height: 12px;
  margin-top: -6px;          /* centre vertical exact, indépendant du border */
  border-radius: 50%;
  background: var(--ink-soft);
  transition: transform 0.15s ease, background 0.15s ease;
}
.toggle:checked {
  background: rgba(95, 191, 108, 0.18);
  border-color: var(--ok);
}
.toggle:checked::after {
  transform: translateX(16px); /* 34 - 2(border) - 2(left) - 12(dot) - 2(gap droit) = 16 */
  background: var(--ok);
}

/* ---- Modal ---------------------------------------------------- */
.modal {
  position: fixed; inset: 0;
  background: rgba(0, 0, 0, 0.7);
  backdrop-filter: blur(4px);
  display: flex; align-items: center; justify-content: center;
  z-index: 50;
  padding: 3vh 3vw;
}
.modal-card {
  background: var(--bg-2);
  border: 1px solid var(--line);
  border-top: 2px solid var(--accent);
  padding: 24px;
  width: 40%;
  min-width: 280px;
  max-width: 100%;
  display: flex;
  flex-direction: column;
  gap: 14px;
}
.modal-card h3 {
  font-size: 12px;
  letter-spacing: 0.32em;
  text-transform: uppercase;
  color: var(--accent);
  font-weight: 600;
}
.modal-card form {
  display: flex; flex-direction: column; gap: 14px;
}
.modal-actions {
  display: flex;
  justify-content: flex-end;
  gap: 8px;
  margin-top: 4px;
}
.warn {
  color: var(--warn);
  font-size: 12px;
  letter-spacing: 0.05em;
}
.key-display {
  display: flex;
  gap: 8px;
  align-items: stretch;
}
.key-display code {
  flex: 1;
  background: var(--bg);
  border: 1px solid var(--line);
  padding: 12px;
  font-family: var(--mono);
  font-size: 13px;
  color: var(--accent);
  word-break: break-all;
  user-select: all;
}

/* ---- Test APIs panel ----------------------------------------- */

.test-panel {
  background: var(--bg-2);
  border: 1px solid var(--line);
  padding: 20px;
  display: flex;
  flex-direction: column;
  gap: 16px;
}
.test-panel > label { width: 100%; }

.tabs {
  display: flex;
  gap: 4px;
  border-bottom: 1px solid var(--line);
}
.tabs .tab {
  font-family: var(--mono);
  font-size: 11px;
  letter-spacing: 0.18em;
  text-transform: uppercase;
  background: transparent;
  border: none;
  color: var(--ink-hint);
  padding: 8px 14px;
  cursor: pointer;
  border-bottom: 2px solid transparent;
  margin-bottom: -1px;
}
.tabs .tab:hover { color: var(--ink-soft); }
.tabs .tab.active {
  color: var(--accent);
  border-bottom-color: var(--accent);
}

/* Sub-tabs (à l'intérieur d'un tab-panel) : plus discret. */
.subtabs {
  border-bottom-color: var(--bg-3);
  margin-top: -4px;
}
.subtabs .tab {
  font-size: 10px;
  letter-spacing: 0.16em;
  padding: 6px 12px;
  color: var(--ink-hint);
}
.subtabs .tab.active {
  color: var(--ink);
  border-bottom-color: var(--ink-soft);
}

.btn-inline {
  padding: 2px 8px;
  font-size: 10px;
  margin-left: 8px;
  vertical-align: middle;
}

#voices-table {
  width: 100%;
  border-collapse: collapse;
  background: var(--bg);
  border: 1px solid var(--line);
  font-size: 12px;
}
#voices-table th, #voices-table td {
  padding: 8px 10px;
  text-align: left;
  border-bottom: 1px solid var(--line);
  vertical-align: middle;
}
#voices-table th {
  font-size: 10px;
  letter-spacing: 0.18em;
  text-transform: uppercase;
  color: var(--ink-hint);
  font-weight: 600;
  background: var(--bg-3);
}
#voices-table tbody tr:hover { background: var(--bg-2); }
#voices-table tr.selected { background: rgba(95, 191, 108, 0.08); }
#voices-table tr.selected td:first-child { border-left: 2px solid var(--ok); }
#voices-table .voice-id {
  font-size: 10px;
  color: var(--ink-soft);
  word-break: break-all;
}
#voices-table .meta {
  font-size: 10px;
  color: var(--ink-hint);
}
#voices-table .selected-mark { color: var(--ok); }

.tab-panel { display: flex; flex-direction: column; gap: 14px; }

textarea {
  background: var(--bg);
  border: 1px solid var(--line);
  color: var(--ink);
  font-family: var(--mono);
  font-size: 14px;
  padding: 10px 12px;
  outline: none;
  resize: vertical;
  min-height: 60px;
  transition: border-color 0.15s ease, box-shadow 0.15s ease;
}
textarea:focus {
  border-color: var(--accent);
  box-shadow: 0 0 0 2px rgba(255, 59, 59, 0.18);
}

.rec-row {
  display: flex;
  align-items: center;
  gap: 12px;
  flex-wrap: wrap;
}
.rec-row #rec-toggle.recording {
  color: white;
  background: var(--accent);
  border-color: var(--accent);
  animation: pulse 1.2s ease-in-out infinite;
}
@keyframes pulse {
  0%,100% { box-shadow: 0 0 0 0 rgba(255, 59, 59, 0.5); }
  50%     { box-shadow: 0 0 0 6px rgba(255, 59, 59, 0); }
}
.rec-status {
  font-size: 11px;
  letter-spacing: 0.18em;
  text-transform: uppercase;
  color: var(--ink-hint);
}
.rec-status.recording { color: var(--accent); }
.rec-status.ready     { color: var(--ok); }
.rec-status.sending   { color: var(--warn); }
.rec-status.error     { color: var(--accent); }

.rec-row audio {
  height: 32px;
  flex: 1;
  min-width: 200px;
  filter: var(--audio-filter);
}
.rec-row .rec-or {
  font-size: 10px;
  letter-spacing: 0.18em;
  text-transform: uppercase;
  color: var(--ink-hint);
}
.file-input { display: inline-flex; cursor: pointer; }
.file-input .btn-ghost { display: inline-block; }
audio + button { margin-top: 8px; }

label.inline-checkbox {
  flex-direction: row;
  align-items: center;
  gap: 8px;
  text-transform: none;
  letter-spacing: 0.04em;
  font-size: 12px;
  color: var(--ink-soft);
  cursor: pointer;
}
label.inline-checkbox input { width: auto; margin: 0; }

.result-block { display: flex; flex-direction: column; gap: 6px; }
.result-label {
  font-size: 11px;
  letter-spacing: 0.18em;
  text-transform: uppercase;
  color: var(--ink-soft);
}
.result-pre {
  background: var(--bg);
  border: 1px solid var(--line);
  padding: 12px;
  font-family: var(--mono);
  font-size: 13px;
  color: var(--ink);
  white-space: pre-wrap;
  word-break: break-word;
  min-height: 48px;
}
.result-pre.error { color: var(--accent); border-color: rgba(255,59,59,0.4); }

/* ---- Adresses : liste de suggestions ----------------------------------- */
.ad-results {
  list-style: none;
  margin: 0;
  padding: 0;
  display: flex;
  flex-direction: column;
  border: 1px solid var(--line);
  background: var(--bg);
}
.ad-results:empty { display: none; }
.ad-result {
  display: flex;
  flex-direction: column;
  gap: 3px;
  padding: 9px 12px;
  border-bottom: 1px solid var(--line);
}
.ad-result:last-child { border-bottom: none; }
.ad-result:hover { background: var(--bg-3); }
.ad-label { font-family: var(--mono); font-size: 13px; color: var(--ink); }
.ad-meta {
  font-size: 10px;
  letter-spacing: 0.12em;
  text-transform: uppercase;
  color: var(--ink-hint);
}

/* ---- Test suite (cellier) ---------------------------------------------- */
.hint {
  font-size: 11px;
  color: var(--ink-hint);
  letter-spacing: 0.04em;
  line-height: 1.6;
}
.hint code {
  background: var(--bg);
  padding: 1px 5px;
  border: 1px solid var(--line);
  font-size: 11px;
  color: var(--ink-soft);
}

.test-log {
  display: flex;
  flex-direction: column;
  gap: 1px;
  background: var(--bg);
  border: 1px solid var(--line);
  font-size: 12px;
  max-height: 60vh;
  overflow-y: auto;
}
.test-log:empty::before {
  content: "(no run yet)";
  display: block;
  padding: 12px;
  color: var(--ink-hint);
  font-style: italic;
  text-align: center;
}
.test-line {
  display: grid;
  grid-template-columns: 24px 80px 60px 1fr 60px;
  gap: 10px;
  padding: 6px 12px;
  border-bottom: 1px solid var(--bg-3);
  align-items: center;
}
.test-line:last-child { border-bottom: none; }
.test-line.ok      { background: rgba(95, 191, 108, 0.04); }
.test-line.fail    { background: rgba(255, 59, 59, 0.08); }
.test-line.running { background: rgba(244, 183, 64, 0.06); }
.test-line .icon {
  font-weight: 700;
  text-align: center;
}
.test-line.ok      .icon { color: var(--ok); }
.test-line.fail    .icon { color: var(--accent); }
.test-line.running .icon { color: var(--warn); }
.test-line .method {
  font-size: 10px;
  letter-spacing: 0.12em;
  text-transform: uppercase;
  color: var(--ink-hint);
}
.test-line .status {
  font-size: 11px;
  color: var(--ink-soft);
}
.test-line.fail .status { color: var(--accent); }
.test-line .label { color: var(--ink); }
.test-line .ms {
  font-size: 10px;
  color: var(--ink-hint);
  text-align: right;
}
.test-line details {
  grid-column: 1 / -1;
  margin-top: 4px;
  margin-left: 34px;
}
.test-line details summary {
  font-size: 10px;
  color: var(--ink-hint);
  cursor: pointer;
  letter-spacing: 0.12em;
  text-transform: uppercase;
}
.test-line details pre {
  margin-top: 4px;
  padding: 8px;
  background: var(--bg-2);
  border: 1px solid var(--line);
  font-size: 11px;
  color: var(--ink-soft);
  white-space: pre-wrap;
  word-break: break-word;
  max-height: 200px;
  overflow-y: auto;
}

/* ---- Misc ----------------------------------------------------- */
.empty {
  color: var(--ink-hint);
  text-align: center;
  padding: 40px;
  font-style: italic;
  font-family: var(--mono);
}
.error {
  background: rgba(255, 59, 59, 0.10);
  border: 1px solid rgba(255, 59, 59, 0.4);
  color: var(--accent);
  padding: 8px 12px;
  font-size: 12px;
  letter-spacing: 0.04em;
}

/* ---- Responsive ---------------------------------------------- */

/* Tablette : on resserre et on laisse les blocs prendre toute la largeur. */
@media (max-width: 720px) {
  header.top { padding: 12px 3%; }
  main { width: 96%; padding: 3vh 0 5vh; }

  /* Sidebar devient horizontale au-dessus du contenu */
  .app { flex-direction: column; gap: 12px; }
  .sidebar {
    flex: none;
    position: static;
    width: 100%;
    border-right: none;
    border-bottom: 1px solid var(--line);
    padding: 0 0 8px 0;
  }
  .nav-vertical { flex-direction: row; gap: 4px; }
  .nav-item {
    border-left: none;
    border-bottom: 2px solid transparent;
    padding: 8px 14px;
  }
  .nav-item.active {
    border-left: none;
    border-bottom-color: var(--accent);
  }

  .bar { flex-wrap: wrap; gap: 10px; }
  .card { width: 100%; min-width: 0; padding: 18px; }
  .modal-card { width: 100%; padding: 18px; }

  /* Table → cards empilées. Chaque <tr> devient un bloc, chaque <td> une ligne
     préfixée par son nom de colonne (injecté en CSS). */
  #keys-table { display: block; border: none; background: transparent; }
  #keys-table thead { display: none; }
  #keys-table tbody { display: block; }
  #keys-table tr {
    display: block;
    background: var(--bg-2);
    border: 1px solid var(--line);
    margin-bottom: 10px;
    padding: 8px 0;
  }
  #keys-table tbody tr:hover { background: var(--bg-2); }
  #keys-table td {
    display: flex;
    align-items: center;
    justify-content: space-between;
    gap: 12px;
    border-bottom: 1px dashed var(--line);
    padding: 8px 14px;
  }
  #keys-table tr td:last-child { border-bottom: none; }
  #keys-table td::before {
    content: attr(data-label);
    font-size: 10px;
    letter-spacing: 0.22em;
    text-transform: uppercase;
    color: var(--ink-hint);
    flex: 0 0 auto;
  }
  /* Labels par position (ordre identique au <thead>). */
  #keys-table td:nth-of-type(1)::before { content: "scope"; }
  #keys-table td:nth-of-type(2)::before { content: "perm"; }
  #keys-table td:nth-of-type(3)::before { content: "label"; }
  #keys-table td:nth-of-type(4)::before { content: "prefix"; }
  #keys-table td:nth-of-type(5)::before { content: "created"; }
  #keys-table td:nth-of-type(6)::before { content: "last used"; }
  #keys-table td:nth-of-type(7)::before { content: "active"; }
  #keys-table td:nth-of-type(8) { justify-content: flex-end; }
  #keys-table td:nth-of-type(8)::before { content: ""; }
  #keys-table .cell-active { width: auto; text-align: right; }
  #keys-table td:nth-of-type(3) { word-break: break-word; }

  .modal { padding: 2vh 2vw; }
}

/* Mobile : header en colonne quand le bouton logout apparaît. */
@media (max-width: 480px) {
  header.top {
    flex-direction: column;
    align-items: flex-start;
    gap: 8px;
    padding: 10px 4%;
  }
  .brand { font-size: 12px; }
  main { width: 100%; padding: 2vh 4% 4vh; }
  .view h2 { letter-spacing: 0.24em; }

  .key-display { flex-direction: column; }
  .key-display code { font-size: 12px; }
  .modal-actions { flex-direction: column-reverse; }
  .modal-actions button { width: 100%; }
}
