:root {
  color-scheme: light;
  --canvas: #f5f5f7;
  --surface: #ffffff;
  --sidebar: #f6f6f8;
  --subtle: #fafafa;
  --fill: #f0f0f2;
  --fill-hover: #e9e9ec;
  --border: #dedee2;
  --border-strong: #c9c9ce;
  --text: #1d1d1f;
  --secondary-text: #6e6e73;
  --tertiary-text: #8e8e93;
  --accent: #0071e3;
  --accent-hover: #0077ed;
  --accent-soft: #e9f2fd;
  --accent-border: #bed9f6;
  --success: #248a3d;
  --success-soft: #edf8f0;
  --warning: #a85d00;
  --warning-soft: #fff7e8;
  --danger: #d70015;
  --danger-soft: #fff0f1;
  --purple: #8944ab;
  --shadow: 0 16px 50px rgba(0, 0, 0, 0.17);
  font-family: -apple-system, BlinkMacSystemFont, "SF Pro Text", "Helvetica Neue", Arial, sans-serif;
}

* { box-sizing: border-box; }
[hidden] { display: none !important; }
html, body { width: 100%; height: 100%; margin: 0; overflow: hidden; }
body {
  background: var(--canvas);
  color: var(--text);
  font-size: 13px;
  line-height: 1.4;
  -webkit-font-smoothing: antialiased;
  text-rendering: optimizeLegibility;
}
button, input, textarea, select { font: inherit; letter-spacing: 0; }
button { cursor: pointer; }
button:disabled { cursor: not-allowed; opacity: 0.42; }
button:focus-visible, input:focus-visible, textarea:focus-visible, select:focus-visible {
  outline: 2px solid var(--accent);
  outline-offset: 2px;
}
input[type="checkbox"] { accent-color: var(--accent); }
svg { width: 17px; height: 17px; flex: 0 0 auto; stroke-width: 1.8; }
::-webkit-scrollbar { width: 10px; height: 10px; }
::-webkit-scrollbar-thumb { background: #c5c5c9; border: 3px solid transparent; border-radius: 8px; background-clip: padding-box; }
::-webkit-scrollbar-track { background: transparent; }

.topbar {
  height: 60px;
  display: grid;
  grid-template-columns: 224px minmax(240px, 520px) minmax(max-content, 1fr);
  align-items: center;
  gap: 16px;
  padding: 0 14px;
  background: #fbfbfc;
  border-bottom: 1px solid var(--border);
}
.brand { min-width: 0; display: flex; align-items: center; gap: 10px; }
.brand-mark {
  width: 32px;
  height: 32px;
  display: grid;
  place-items: center;
  color: #fff;
  background: var(--accent);
  border-radius: 7px;
}
.brand-mark svg { width: 16px; height: 16px; }
.brand > div:last-child { min-width: 0; display: grid; }
.brand strong { overflow: hidden; font-size: 15px; font-weight: 650; text-overflow: ellipsis; white-space: nowrap; }
.brand span { color: var(--secondary-text); font-size: 11px; }

.search-box, .filter-control {
  min-width: 0;
  display: flex;
  align-items: center;
  gap: 7px;
  height: 34px;
  padding: 0 10px;
  color: var(--tertiary-text);
  background: var(--fill);
  border: 1px solid transparent;
  border-radius: 7px;
  transition: background 120ms ease, border-color 120ms ease;
}
.search-box:focus-within { background: var(--surface); border-color: var(--accent); }
.search-box input { width: 100%; min-width: 0; padding: 0; color: var(--text); background: transparent; border: 0; outline: 0; }
.search-box input::placeholder { color: var(--tertiary-text); }
.top-actions, .header-actions, .model-actions { min-width: 0; display: flex; align-items: center; justify-content: flex-end; gap: 6px; }

.primary-button, .secondary-button, .danger-button, .icon-button, .text-button {
  min-height: 32px;
  display: inline-flex;
  align-items: center;
  justify-content: center;
  gap: 6px;
  border: 1px solid transparent;
  border-radius: 6px;
  white-space: nowrap;
  transition: background 100ms ease, border-color 100ms ease, color 100ms ease;
}
.primary-button { padding: 0 12px; color: #fff; background: var(--accent); border-color: var(--accent); font-weight: 550; }
.primary-button:hover:not(:disabled) { background: var(--accent-hover); border-color: var(--accent-hover); }
.primary-button:active:not(:disabled) { background: #0068d1; }
.secondary-button { padding: 0 11px; color: var(--text); background: var(--surface); border-color: var(--border-strong); }
.secondary-button:hover:not(:disabled) { background: var(--fill); }
.secondary-button:active:not(:disabled) { background: var(--fill-hover); }
.danger-button { padding: 0 11px; color: var(--danger); background: var(--surface); border-color: #efb7bd; }
.danger-button:hover:not(:disabled) { background: var(--danger-soft); }
.icon-button { width: 32px; padding: 0; color: var(--secondary-text); background: transparent; }
.icon-button:hover:not(:disabled) { color: var(--text); background: var(--fill-hover); }
.icon-button.small { width: 28px; min-height: 28px; }
.icon-button.small svg { width: 15px; height: 15px; }
.danger-hover:hover:not(:disabled) { color: var(--danger); background: var(--danger-soft); }
.text-button { min-height: 28px; padding: 0 6px; color: var(--accent); background: transparent; }
.text-button:hover:not(:disabled) { background: var(--accent-soft); }
.compact { min-height: 30px; padding: 0 9px; }
.full-width { width: 100%; }

.workspace {
  height: calc(100vh - 60px);
  height: calc(100dvh - 60px);
  display: grid;
  grid-template-columns: clamp(218px, 17vw, 248px) clamp(238px, 19vw, 282px) minmax(0, 1fr);
  background: var(--surface);
}
.pane { min-width: 0; overflow: hidden; background: var(--surface); border-right: 1px solid var(--border); }
.sites-pane { background: var(--sidebar); }
.keys-pane { background: #fbfbfc; }
.pane:last-child { border-right: 0; }
.pane-header {
  height: 52px;
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 8px;
  padding: 0 11px 0 13px;
  border-bottom: 1px solid var(--border);
}
.pane-header > div:first-child { min-width: 0; display: grid; }
.eyebrow { color: var(--tertiary-text); font-size: 10px; font-weight: 600; line-height: 1.25; text-transform: uppercase; }
.pane-header strong, .model-header strong { overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.pane-header strong { font-size: 13px; font-weight: 620; }
.truncate { min-width: 0; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.muted { color: var(--secondary-text); font-size: 11px; }
.push-right { margin-left: auto; }

.list { height: calc(100% - 52px); overflow-y: auto; padding: 5px; }
.list-item {
  width: 100%;
  min-height: 53px;
  display: grid;
  grid-template-columns: 8px minmax(0, 1fr) auto;
  align-items: center;
  gap: 8px;
  padding: 7px 8px;
  color: var(--text);
  text-align: left;
  background: transparent;
  border: 1px solid transparent;
  border-radius: 6px;
}
.list-item:hover { background: rgba(0, 0, 0, 0.035); }
.list-item.active { background: var(--accent-soft); border-color: var(--accent-border); }
.list-item .item-copy { min-width: 0; display: grid; gap: 2px; }
.list-item strong { overflow: hidden; font-size: 13px; font-weight: 590; text-overflow: ellipsis; white-space: nowrap; }
.list-item small { overflow: hidden; color: var(--secondary-text); font-size: 11px; text-overflow: ellipsis; white-space: nowrap; }
.list-item .count { min-width: 22px; padding: 1px 5px; color: var(--secondary-text); background: rgba(0, 0, 0, 0.045); border-radius: 5px; font-size: 10px; text-align: center; }
.site-group { margin: 3px 0 5px; }
.site-group-header {
  min-height: 34px;
  display: grid;
  grid-template-columns: 8px minmax(0, 1fr) auto;
  align-items: center;
  gap: 8px;
  padding: 4px 8px 3px;
  color: var(--secondary-text);
}
.site-group-copy { min-width: 0; display: grid; gap: 1px; }
.site-group-copy strong { overflow: hidden; color: var(--text); font-size: 12px; font-weight: 620; text-overflow: ellipsis; white-space: nowrap; }
.site-group-copy small { overflow: hidden; color: var(--tertiary-text); font-size: 10px; text-overflow: ellipsis; white-space: nowrap; }
.site-group-header .count { min-width: 22px; padding: 1px 5px; color: var(--secondary-text); background: rgba(0, 0, 0, 0.045); border-radius: 5px; font-size: 10px; text-align: center; }
.site-variants { margin-left: 10px; padding-left: 4px; border-left: 1px solid var(--border); }
.site-variant { min-height: 46px; padding-top: 5px; padding-bottom: 5px; }
.status-dot { width: 7px; height: 7px; border-radius: 50%; background: var(--tertiary-text); }
.status-dot.available { background: var(--success); }
.status-dot.warning { background: #d97a00; }
.status-dot.failed { background: var(--danger); }
.list-empty { padding: 34px 14px; color: var(--secondary-text); text-align: center; }

.models-pane { display: grid; grid-template-rows: auto auto minmax(0, 1fr); }
.model-header {
  min-height: 60px;
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 12px;
  padding: 8px 11px 8px 13px;
  border-bottom: 1px solid var(--border);
}
.model-header > div:first-child { min-width: 120px; display: grid; gap: 1px; }
.model-header strong { font-size: 14px; font-weight: 620; }
.table-toolbar {
  min-height: 42px;
  display: flex;
  align-items: center;
  gap: 8px;
  padding: 5px 10px;
  background: var(--subtle);
  border-bottom: 1px solid var(--border);
}
.filter-control { height: 30px; padding: 0 7px; background: var(--surface); border-color: var(--border); border-radius: 6px; }
.filter-control svg { width: 15px; height: 15px; }
.filter-control select { min-width: 88px; padding: 0 16px 0 0; color: var(--text); background: transparent; border: 0; outline: 0; }
.table-wrap { min-height: 0; position: relative; overflow: auto; }
table { width: 100%; border-collapse: collapse; table-layout: fixed; }
th {
  height: 36px;
  padding: 0 9px;
  position: sticky;
  top: 0;
  z-index: 2;
  color: var(--secondary-text);
  background: rgba(255, 255, 255, 0.97);
  border-bottom: 1px solid var(--border);
  font-size: 10px;
  font-weight: 600;
  text-align: left;
}
td { height: 49px; padding: 5px 9px; border-bottom: 1px solid #eeeeef; vertical-align: middle; }
tbody tr:hover { background: var(--subtle); }
th:nth-child(2) { width: 31%; }
th:nth-child(3) { width: 78px; }
th:nth-child(4) { width: 104px; }
th:nth-child(5) { width: 74px; }
th:nth-child(6) { width: 128px; }
.check-column { width: 38px; text-align: center; }
.actions-column { width: 68px; }
.model-name { min-width: 0; display: grid; gap: 1px; }
.model-name strong { overflow: hidden; font-size: 12px; font-weight: 590; text-overflow: ellipsis; white-space: nowrap; }
.model-name small { overflow: hidden; color: var(--secondary-text); font-size: 10px; text-overflow: ellipsis; white-space: nowrap; }
.row-actions { display: flex; justify-content: flex-end; gap: 1px; }
.badge {
  min-height: 21px;
  max-width: 100%;
  display: inline-flex;
  align-items: center;
  padding: 0 6px;
  overflow: hidden;
  color: var(--secondary-text);
  background: var(--fill);
  border: 1px solid var(--border);
  border-radius: 5px;
  font-size: 10px;
  text-overflow: ellipsis;
  white-space: nowrap;
}
.badge.available { color: #176c2d; background: var(--success-soft); border-color: #baddc2; }
.badge.unavailable, .badge.auth_failed { color: #a20c1d; background: var(--danger-soft); border-color: #efc3c8; }
.badge.rate_limited { color: var(--warning); background: var(--warning-soft); border-color: #ecd3aa; }
.badge.unsupported { color: #636366; background: #f1f1f3; border-color: #d8d8dc; }
.badge.untested { color: var(--secondary-text); }
.category-image { color: var(--purple); background: #f7effa; border-color: #dfc6ea; }
.category-video { color: #315f98; background: #eef5fc; border-color: #c8dbef; }
.category-text { color: #176c2d; background: var(--success-soft); border-color: #baddc2; }
.empty-table { padding: 70px 20px; color: var(--secondary-text); text-align: center; }

.mobile-tabs { display: none; }
.job-panel {
  width: min(390px, calc(100vw - 32px));
  display: grid;
  grid-template-columns: minmax(0, 1fr) 32px;
  gap: 8px 10px;
  position: fixed;
  right: 16px;
  bottom: 16px;
  z-index: 20;
  padding: 11px 12px;
  color: #fff;
  background: #29292c;
  border: 1px solid #3b3b3f;
  border-radius: 8px;
  box-shadow: 0 8px 24px rgba(0, 0, 0, 0.22);
}
.job-panel > div { display: flex; justify-content: space-between; gap: 10px; }
.job-panel progress { width: 100%; grid-column: 1; accent-color: #0a84ff; }
.job-panel .icon-button { grid-column: 2; grid-row: 1 / span 2; align-self: center; color: #fff; }
.job-panel .icon-button:hover { background: rgba(255, 255, 255, 0.12); }

.modal {
  width: min(640px, calc(100vw - 28px));
  max-height: min(88vh, 900px);
  padding: 0;
  overflow: auto;
  color: var(--text);
  background: var(--surface);
  border: 1px solid var(--border-strong);
  border-radius: 8px;
  box-shadow: var(--shadow);
}
.modal::backdrop { background: rgba(0, 0, 0, 0.34); }
.small-modal { width: min(480px, calc(100vw - 28px)); }
.wide-modal { width: min(1030px, calc(100vw - 28px)); }
.modal-header {
  min-height: 58px;
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 10px;
  padding: 10px 12px 10px 16px;
  background: #fbfbfc;
  border-bottom: 1px solid var(--border);
}
.modal-header h2 { margin: 1px 0 0; font-size: 17px; font-weight: 650; }
.modal-actions {
  min-height: 56px;
  display: flex;
  align-items: center;
  justify-content: flex-end;
  gap: 7px;
  padding: 11px 16px;
  background: #fbfbfc;
  border-top: 1px solid var(--border);
}
.form-grid { display: grid; gap: 13px; padding: 16px; }
.two-columns { grid-template-columns: repeat(2, minmax(0, 1fr)); }
.three-columns { grid-template-columns: repeat(3, minmax(0, 1fr)); padding: 0; }
.form-grid label { min-width: 0; display: grid; gap: 5px; }
.form-grid label > span { color: var(--secondary-text); font-size: 11px; font-weight: 500; }
.form-grid input, .form-grid textarea, .form-grid select, .secret-output textarea {
  width: 100%;
  min-width: 0;
  padding: 8px 9px;
  color: var(--text);
  background: var(--surface);
  border: 1px solid var(--border-strong);
  border-radius: 6px;
  outline: 0;
}
.form-grid input, .form-grid select { min-height: 34px; }
.form-grid input:focus, .form-grid textarea:focus, .form-grid select:focus, .secret-output textarea:focus { border-color: var(--accent); box-shadow: 0 0 0 2px rgba(0, 113, 227, 0.14); }
.form-grid textarea, .secret-output textarea { resize: vertical; font-family: "SFMono-Regular", Consolas, "Liberation Mono", monospace; font-size: 12px; line-height: 1.45; }
.form-grid .full { grid-column: 1 / -1; }
.form-grid .span-two { grid-column: span 2; }
.checkbox-label { display: flex !important; align-items: center; gap: 7px !important; }
.checkbox-label input { width: 16px; min-height: 16px; height: 16px; }
.secret-actions { display: flex; justify-content: space-between; gap: 8px; padding: 0 16px 16px; }

.template-layout { height: min(680px, calc(88vh - 59px)); display: grid; grid-template-columns: 220px minmax(0, 1fr); }
.template-sidebar { padding: 11px; overflow-y: auto; background: var(--sidebar); border-right: 1px solid var(--border); }
.template-list { display: grid; gap: 2px; margin-top: 9px; }
.template-list button { width: 100%; padding: 8px; color: var(--text); text-align: left; background: transparent; border: 1px solid transparent; border-radius: 6px; }
.template-list button:hover { background: rgba(0, 0, 0, 0.035); }
.template-list button.active { background: var(--accent-soft); border-color: var(--accent-border); }
.template-list small { display: block; margin-top: 1px; color: var(--secondary-text); font-size: 10px; }
.template-form { min-width: 0; display: grid; grid-template-rows: minmax(0, 1fr) auto; padding: 16px 16px 0; overflow: hidden; }
.template-form .form-grid { overflow-y: auto; padding-right: 6px; }
.template-actions { margin: 0 -16px; }
.template-actions .danger-button { margin-right: auto; }

.audit-list { max-height: 66vh; overflow: auto; }
.audit-row { min-height: 44px; display: grid; grid-template-columns: 150px 132px minmax(0, 1fr) 82px; gap: 10px; align-items: center; padding: 7px 16px; border-bottom: 1px solid #eeeeef; }
.audit-row code { overflow: hidden; color: var(--secondary-text); font-size: 11px; text-overflow: ellipsis; white-space: nowrap; }
.audit-result { justify-self: end; }
.backup-state { margin: 16px; padding: 11px; background: var(--subtle); border: 1px solid var(--border); border-radius: 6px; }
.backup-state.warning { color: var(--warning); background: var(--warning-soft); border-color: #ecd3aa; }
.secret-output { display: grid; grid-template-columns: minmax(0, 1fr) auto; gap: 8px; padding: 0 16px 16px; }

.toast-stack { width: min(340px, calc(100vw - 28px)); display: grid; gap: 7px; position: fixed; top: 70px; right: 14px; z-index: 100; }
.toast { padding: 10px 11px; color: #fff; background: #29292c; border: 1px solid #3b3b3f; border-radius: 7px; box-shadow: 0 7px 22px rgba(0, 0, 0, 0.2); animation: toast-in 150ms ease-out; }
.toast.error { background: #a50e20; border-color: #bf3544; }
@keyframes toast-in { from { transform: translateY(-6px); opacity: 0; } }

.login-page {
  min-height: 100vh;
  overflow: auto;
  background: var(--canvas);
}
.login-shell {
  min-height: 100vh;
  display: grid;
  place-items: center;
  padding: 24px;
}
.login-panel {
  width: min(360px, 100%);
  padding: 30px;
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: 10px;
  box-shadow: 0 14px 42px rgba(0, 0, 0, 0.08);
}
.login-mark {
  width: 42px;
  height: 42px;
  display: grid;
  place-items: center;
  color: #fff;
  background: var(--accent);
  border-radius: 9px;
}
.login-heading { margin: 18px 0 26px; }
.login-heading h1 { margin: 0; font-size: 24px; font-weight: 650; }
.login-heading p { margin: 5px 0 0; color: var(--secondary-text); }
.login-form { display: grid; gap: 9px; }
.login-form > label { color: var(--secondary-text); font-size: 12px; }
.password-field { position: relative; }
.password-field input {
  width: 100%;
  height: 42px;
  padding: 0 44px 0 11px;
  color: var(--text);
  background: var(--surface);
  border: 1px solid var(--border-strong);
  border-radius: 7px;
  outline: 0;
}
.password-field input:focus { border-color: var(--accent); box-shadow: 0 0 0 3px var(--accent-soft); }
.password-field .icon-button { position: absolute; top: 6px; right: 6px; }
.login-submit { width: 100%; height: 40px; margin-top: 8px; }
.login-error {
  padding: 9px 10px;
  color: var(--danger);
  background: var(--danger-soft);
  border: 1px solid #efc3c8;
  border-radius: 7px;
  font-size: 12px;
}

@media (max-width: 1120px) {
  .topbar { grid-template-columns: 208px minmax(180px, 1fr) auto; gap: 12px; padding-inline: 12px; }
  .top-actions .secondary-button { width: 32px; padding: 0; }
  .top-actions .secondary-button span { display: none; }
  .workspace { grid-template-columns: 210px 230px minmax(0, 1fr); }
  .model-actions .secondary-button, .model-actions .primary-button { width: 30px; padding: 0; }
  .model-actions .secondary-button span, .model-actions .primary-button span { display: none; }
  th:nth-child(6), td:nth-child(6) { display: none; }
}

@media (max-width: 860px) and (min-width: 761px) {
  .topbar { grid-template-columns: 190px minmax(150px, 1fr) auto; }
  .workspace { grid-template-columns: 190px 215px minmax(0, 1fr); }
  th:nth-child(3), td:nth-child(3) { display: none; }
}

@media (max-width: 760px) {
  html, body { overflow: hidden; }
  .topbar { height: 56px; grid-template-columns: minmax(0, 1fr) auto; gap: 8px; padding: 0 10px; }
  .brand { gap: 8px; }
  .brand-mark { width: 30px; height: 30px; }
  .brand span, .search-box, .top-actions .icon-button, .top-actions .secondary-button { display: none; }
  .top-actions #vaultBtn:not([hidden]), .top-actions #logoutBtn:not([hidden]) { width: 32px; display: inline-flex; }
  .top-actions { grid-column: 2; }
  .top-actions .primary-button { width: 34px; min-height: 32px; padding: 0; }
  .top-actions .primary-button span { display: none; }
  .workspace { height: calc(100vh - 110px); height: calc(100dvh - 110px); display: block; }
  .pane { display: none; width: 100%; height: 100%; border-right: 0; }
  .pane.mobile-active { display: block; }
  .models-pane.mobile-active { display: grid; grid-template-rows: auto auto minmax(0, 1fr); }
  .mobile-tabs {
    height: 54px;
    display: grid;
    grid-template-columns: repeat(3, 1fr);
    padding-bottom: env(safe-area-inset-bottom);
    background: #fbfbfc;
    border-top: 1px solid var(--border);
  }
  .mobile-tabs button { min-width: 0; display: grid; place-items: center; align-content: center; gap: 1px; color: var(--secondary-text); background: transparent; border: 0; }
  .mobile-tabs button.active { color: var(--accent); background: var(--accent-soft); }
  .mobile-tabs svg { width: 16px; height: 16px; }
  .mobile-tabs span { font-size: 10px; }
  .model-header { min-height: 96px; align-items: flex-start; flex-direction: column; gap: 7px; padding-block: 8px; }
  .model-header > div:first-child { width: 100%; }
  .model-actions { width: 100%; justify-content: flex-start; }
  .model-actions .secondary-button, .model-actions .primary-button { width: 32px; min-height: 30px; }
  .table-toolbar { min-height: 42px; gap: 6px; overflow-x: auto; }
  .table-toolbar .text-button { flex: 0 0 auto; }
  .table-toolbar .push-right { flex: 0 0 auto; }
  table { min-width: 0; }
  th:nth-child(2) { width: auto; }
  th:nth-child(3), td:nth-child(3),
  th:nth-child(5), td:nth-child(5),
  th:nth-child(6), td:nth-child(6) { display: none; }
  th:nth-child(4) { width: 82px; }
  .check-column { width: 38px; }
  .actions-column { width: 66px; }
  .job-panel { right: 12px; bottom: 66px; width: calc(100vw - 24px); }
  .modal { width: calc(100vw - 20px); max-height: calc(100dvh - 20px); }
  .wide-modal { height: calc(100dvh - 20px); overflow: hidden; }
  .two-columns, .three-columns { grid-template-columns: 1fr; }
  .form-grid .span-two { grid-column: auto; }
  .modal-actions { flex-wrap: wrap; }
  .template-layout { height: calc(100% - 59px); grid-template-columns: 1fr; grid-template-rows: 150px minmax(0, 1fr); }
  .template-sidebar { max-height: none; border-right: 0; border-bottom: 1px solid var(--border); }
  .template-form { min-height: 0; padding: 12px 12px 0; }
  .template-actions { margin: 0 -12px; }
  .audit-row { grid-template-columns: 1fr auto; }
  .audit-row code, .audit-row span:nth-child(3) { grid-column: 1 / -1; }
  .secret-output { grid-template-columns: 1fr; }
}

@media (prefers-reduced-motion: reduce) {
  *, *::before, *::after { scroll-behavior: auto !important; animation-duration: 0.01ms !important; transition-duration: 0.01ms !important; }
}
