:root {
  --bg: #f4f6f8;
  --surface: #ffffff;
  --text: #111111;
  --muted: #5f6368;
  --line: #e6e8eb;
  --line-strong: #d7dbe0;
  --blue: #1a56db;
  --blue-soft: rgba(26, 86, 219, 0.08);
  --ok: #0f7b3a;
  --ok-bg: #e8f6ee;
  --danger: #b42318;
  --danger-bg: #fdecea;
  --shadow: 0 1px 2px rgba(17, 17, 17, 0.04), 0 12px 32px rgba(17, 17, 17, 0.06);
  --shadow-lg: 0 24px 60px rgba(17, 17, 17, 0.12);
  --radius: 14px;
  --ease: cubic-bezier(0.22, 1, 0.36, 1);
  font-family: "DM Sans", "Segoe UI", system-ui, sans-serif;
}

*,
*::before,
*::after { box-sizing: border-box; }

html {
  overflow-x: clip;
  -webkit-text-size-adjust: 100%;
}

html, body {
  margin: 0;
  min-height: 100%;
  background: var(--bg);
  color: var(--text);
}

body.nav-open {
  overflow: hidden;
  touch-action: none;
}

body {
  font-size: 15px;
  line-height: 1.45;
  letter-spacing: -0.011em;
}

button, input, select, textarea { font: inherit; color: inherit; }
button { cursor: pointer; }
.hidden { display: none !important; }

.error {
  color: var(--danger);
  background: var(--danger-bg);
  border-radius: 10px;
  padding: 10px 12px;
  font-size: 13px;
  margin: 0 0 14px;
}
.error.ok {
  color: var(--ok);
  background: var(--ok-bg);
}

/* ---------- install ---------- */
.install-banner {
  position: sticky;
  top: 0;
  z-index: 50;
  display: flex;
  gap: 12px;
  align-items: center;
  justify-content: space-between;
  padding: 10px 20px;
  background: var(--surface);
  border-bottom: 1px solid var(--line);
  animation: slideDown 0.35s var(--ease);
}
.install-banner span { color: var(--muted); font-size: 13px; flex: 1; min-width: 0; }
.install-actions { display: flex; gap: 8px; flex-shrink: 0; flex-wrap: wrap; }
.install-banner button {
  background: var(--text);
  color: #fff;
  border: 0;
  border-radius: 10px;
  padding: 8px 14px;
  font-weight: 600;
}
.install-banner .ghost {
  background: transparent;
  color: var(--muted);
  border: 1px solid var(--line);
}

/* ---------- login ---------- */
.login-view {
  min-height: 100dvh;
  display: grid;
  place-items: center;
  padding: 32px 20px;
  background:
    linear-gradient(180deg, #fff 0%, var(--bg) 42%);
}
.login-shell {
  width: min(920px, 100%);
  display: grid;
  grid-template-columns: 1.05fr 0.95fr;
  background: var(--surface);
  border: 1px solid var(--line);
  border-radius: 22px;
  overflow: hidden;
  box-shadow: var(--shadow-lg);
  animation: rise 0.55s var(--ease);
}
.login-aside {
  padding: 40px 36px;
  background:
    linear-gradient(165deg, #fff 0%, #f7f9fc 58%, var(--blue-soft) 100%);
  border-right: 1px solid var(--line);
}
.login-aside h2 {
  margin: 36px 0 12px;
  font-size: 28px;
  line-height: 1.2;
  letter-spacing: -0.03em;
  font-weight: 650;
}
.login-copy { margin: 0; color: var(--muted); max-width: 34ch; }
.login-card { padding: 40px 36px; }
.login-card h3 { margin: 0 0 6px; font-size: 24px; letter-spacing: -0.03em; }
.login-lead { margin: 0 0 22px; color: var(--muted); }
.remember-me {
  display: flex;
  align-items: center;
  gap: 10px;
  margin: 0 0 16px;
  font-size: 14px;
  color: var(--muted);
  cursor: pointer;
  user-select: none;
}
.remember-me input {
  width: 16px;
  height: 16px;
  margin: 0;
  accent-color: var(--blue);
}
.eyebrow {
  margin: 0 0 10px;
  font-size: 11px;
  font-weight: 700;
  letter-spacing: 0.14em;
  text-transform: uppercase;
  color: var(--muted);
}

.brand { display: flex; gap: 12px; align-items: center; margin-bottom: 8px; }
.brand.compact { margin: 0 0 28px; }
.logo {
  width: 42px;
  height: 42px;
  border-radius: 12px;
  display: grid;
  place-items: center;
  background: #fff;
  color: var(--text);
  border: 1.5px solid var(--text);
  font-weight: 760;
  letter-spacing: -0.04em;
}
.logo-img {
  width: 46px;
  height: 46px;
  object-fit: contain;
  background: #fff;
  border: 1px solid var(--line);
  border-radius: 12px;
  padding: 4px;
  flex: 0 0 auto;
}
.brand h1, .brand strong { margin: 0; font-size: 20px; letter-spacing: -0.03em; }
.brand p, .brand small { margin: 2px 0 0; color: var(--muted); display: block; font-size: 13px; }

label {
  display: grid;
  gap: 6px;
  margin-bottom: 14px;
  color: var(--muted);
  font-size: 13px;
  font-weight: 550;
}
input, select, textarea {
  width: 100%;
  background: #fff;
  color: var(--text);
  border: 1px solid var(--line-strong);
  border-radius: 11px;
  padding: 12px 13px;
  outline: none;
  transition: border-color 0.18s ease, box-shadow 0.18s ease, transform 0.18s ease;
}

.hint {
  margin: -6px 0 16px;
  color: var(--muted);
  font-size: 12px;
  line-height: 1.4;
}
input:hover, select:hover { border-color: #c5cad1; }
input:focus, select:focus, textarea:focus {
  border-color: var(--blue);
  box-shadow: 0 0 0 3px var(--blue-soft);
}

button[type="submit"], .toolbar button, .primary, .modal-actions button:not(.ghost) {
  background: var(--text);
  color: #fff;
  border: 0;
  border-radius: 11px;
  padding: 12px 16px;
  font-weight: 650;
  transition: transform 0.18s var(--ease), background 0.18s ease, opacity 0.18s ease;
}
button[type="submit"] { width: 100%; }
.pwa-install {
  width: 100%;
  margin-top: 10px;
  background: transparent !important;
  color: var(--text) !important;
  border: 1px solid var(--line) !important;
}
button[type="submit"]:hover,
.toolbar button:hover,
.primary:hover,
.modal-actions button:not(.ghost):hover { background: #000; }
button[type="submit"]:active,
.toolbar button:active { transform: translateY(1px) scale(0.99); }

/* ---------- app shell ---------- */
.app-view {
  min-height: 100dvh;
  display: grid;
  grid-template-columns: 260px minmax(0, 1fr);
  grid-template-areas: "sidebar main";
  align-items: start;
  animation: fade 0.4s var(--ease);
  isolation: isolate;
}
.app-view > .sidebar {
  grid-area: sidebar;
  align-self: start;
  position: sticky;
  top: 0;
  z-index: 10;
  width: 260px;
  max-width: 260px;
}
.app-view > main {
  grid-area: main;
  min-width: 0;
  width: 100%;
}
.sidebar {
  background: var(--surface);
  border-right: 1px solid var(--line);
  padding: 22px 14px;
  padding-bottom: max(22px, env(safe-area-inset-bottom));
  display: flex;
  flex-direction: column;
  gap: 6px;
  min-height: 100dvh;
  max-height: 100dvh;
  overflow-y: auto;
  overscroll-behavior: contain;
  -webkit-overflow-scrolling: touch;
}
.sidebar nav { display: grid; gap: 4px; flex: 1; }
.sidebar nav button {
  display: flex;
  align-items: center;
  gap: 10px;
  background: transparent;
  color: var(--muted);
  border: 0;
  text-align: left;
  padding: 11px 12px;
  border-radius: 11px;
  font-weight: 550;
  transition: background 0.18s ease, color 0.18s ease, transform 0.18s var(--ease);
}
.sidebar nav button svg {
  width: 18px;
  height: 18px;
  fill: none;
  stroke: currentColor;
  stroke-width: 1.7;
  stroke-linecap: round;
  stroke-linejoin: round;
  flex: 0 0 18px;
}
.sidebar nav button:hover { background: #f3f4f6; color: var(--text); }
.sidebar nav button.active {
  background: #111;
  color: #fff;
}
.logout {
  background: transparent;
  color: var(--muted);
  border: 1px solid var(--line);
  text-align: left;
  padding: 11px 12px;
  border-radius: 11px;
  font-weight: 550;
  transition: color 0.18s ease, border-color 0.18s ease, background 0.18s ease;
}
.logout:hover { color: var(--danger); border-color: #f0b8b4; background: var(--danger-bg); }

main {
  padding: 22px 28px 40px;
  min-width: 0;
  max-width: 100%;
  overflow-x: clip;
}
.topbar {
  display: flex;
  align-items: center;
  gap: 14px;
  margin-bottom: 22px;
  flex-wrap: wrap;
  row-gap: 10px;
}
.topbar-copy { flex: 1; min-width: 0; }
.topbar h2 { margin: 0; font-size: 26px; letter-spacing: -0.04em; }
.topbar p { margin: 3px 0 0; color: var(--muted); font-size: 13px; }
.menu {
  display: none;
  width: 42px;
  height: 42px;
  border: 1px solid var(--line);
  background: #fff;
  border-radius: 11px;
  padding: 11px 10px;
  flex-direction: column;
  justify-content: space-between;
}
.menu span {
  display: block;
  height: 1.5px;
  background: var(--text);
  border-radius: 2px;
}
.chip {
  background: #fff;
  border: 1px solid var(--line);
  padding: 8px 12px;
  border-radius: 999px;
  color: var(--text);
  font-size: 13px;
  font-weight: 600;
}

.page { animation: pageIn 0.38s var(--ease); }

.stats {
  display: grid;
  grid-template-columns: repeat(4, 1fr);
  gap: 14px;
  margin-bottom: 18px;
}
.stats article {
  background: var(--surface);
  border: 1px solid var(--line);
  border-radius: var(--radius);
  padding: 18px 18px 16px;
  box-shadow: var(--shadow);
  animation: rise 0.45s var(--ease) both;
}
.stats article:nth-child(1) { animation-delay: 0.02s; }
.stats article:nth-child(2) { animation-delay: 0.08s; }
.stats article:nth-child(3) { animation-delay: 0.14s; }
.stats article:nth-child(4) { animation-delay: 0.2s; }
.stats article:hover { transform: translateY(-2px); box-shadow: var(--shadow-lg); }
.stats article { transition: transform 0.22s var(--ease), box-shadow 0.22s ease; }
.stats span {
  color: var(--muted);
  font-size: 12px;
  font-weight: 650;
  letter-spacing: 0.04em;
  text-transform: uppercase;
}
.stats strong {
  display: block;
  font-size: 32px;
  margin-top: 10px;
  letter-spacing: -0.05em;
  font-weight: 700;
}

.panel, .modal-card {
  background: var(--surface);
  border: 1px solid var(--line);
  border-radius: var(--radius);
  box-shadow: var(--shadow);
}
.panel { padding: 18px 18px 8px; animation: rise 0.5s var(--ease) both; animation-delay: 0.12s; }
.panel.flush {
  padding: 0;
  overflow: hidden;
  width: 100%;
  max-width: 100%;
}
#page-tokens .table-wrap {
  width: 100%;
  max-width: 100%;
}
.panel.narrow { max-width: 460px; padding: 22px; }
.panel-head { margin-bottom: 8px; }
.panel-head h3 { margin: 0; font-size: 16px; letter-spacing: -0.02em; }
.panel-head p { margin: 4px 0 0; color: var(--muted); font-size: 13px; }

.toolbar { display: flex; gap: 10px; margin-bottom: 14px; }
.toolbar input { flex: 1; background: #fff; }
.toolbar button { width: auto; padding-inline: 18px; }

.table-wrap {
  overflow-x: auto;
  overflow-y: hidden;
  -webkit-overflow-scrolling: touch;
  max-width: 100%;
}
table { width: 100%; min-width: 520px; border-collapse: collapse; font-size: 14px; }
th, td {
  text-align: left;
  padding: 13px 12px;
  border-bottom: 1px solid var(--line);
  white-space: nowrap;
}
td.token-key {
  white-space: normal;
  word-break: break-all;
  max-width: 14rem;
}
.panel.flush .token-cards {
  min-width: 0;
}
th {
  color: var(--muted);
  font-weight: 650;
  font-size: 12px;
  letter-spacing: 0.04em;
  text-transform: uppercase;
  background: #fafbfc;
}
tbody tr { animation: fade 0.35s var(--ease) both; }
tbody tr:nth-child(1) { animation-delay: 0.02s; }
tbody tr:nth-child(2) { animation-delay: 0.05s; }
tbody tr:nth-child(3) { animation-delay: 0.08s; }
tbody tr:nth-child(4) { animation-delay: 0.11s; }
tbody tr:hover { background: #fafbfc; }
.badge {
  padding: 4px 8px;
  border-radius: 999px;
  font-size: 12px;
  font-weight: 650;
  text-transform: capitalize;
}
.badge.on { background: var(--ok-bg); color: var(--ok); }
.badge.off { background: var(--danger-bg); color: var(--danger); }
.row-actions { display: flex; gap: 6px; }
.row-actions button {
  background: #fff;
  color: var(--text);
  border: 1px solid var(--line-strong);
  border-radius: 8px;
  padding: 6px 10px;
  font-size: 12px;
  font-weight: 600;
  transition: background 0.15s ease, border-color 0.15s ease;
}
.row-actions button:hover { background: #f3f4f6; border-color: #c5cad1; }
.row-actions button[data-del]:hover,
.row-actions button[data-remove-device]:hover {
  color: var(--danger);
  border-color: #f0b8b4;
  background: var(--danger-bg);
}

.modal {
  position: fixed;
  inset: 0;
  background: rgba(17, 17, 17, 0.28);
  display: grid;
  place-items: center;
  padding: 16px;
  z-index: 40;
  backdrop-filter: blur(6px);
  animation: fade 0.22s ease;
}
.modal-card {
  width: min(720px, 100%);
  padding: 24px;
  max-height: 90dvh;
  overflow: auto;
  animation: pop 0.32s var(--ease);
}
.modal-card h3 { margin-top: 0; letter-spacing: -0.03em; }
.modal-actions { display: flex; gap: 8px; justify-content: flex-end; margin-top: 8px; }
.modal-actions button { width: auto; }
.ghost {
  background: transparent !important;
  color: var(--muted) !important;
  border: 1px solid var(--line) !important;
}
.ghost:hover { background: #f3f4f6 !important; color: var(--text) !important; }
.token-key {
  font-family: ui-monospace, Consolas, monospace;
  letter-spacing: 0.4px;
  background: #f4f6f8;
  padding: 8px 10px;
  border-radius: 8px;
  display: inline-block;
}
.empty, .empty-state {
  color: var(--muted);
  padding: 36px 16px;
  text-align: center;
}
.empty-state strong { display: block; color: var(--text); margin-bottom: 6px; }

.toast {
  position: fixed;
  right: 20px;
  bottom: 20px;
  z-index: 60;
  background: var(--text);
  color: #fff;
  padding: 12px 16px;
  border-radius: 12px;
  box-shadow: var(--shadow-lg);
  animation: toastIn 0.28s var(--ease);
}
.sidebar-backdrop {
  display: none;
  pointer-events: none;
}

@keyframes rise {
  from { opacity: 0; transform: translateY(12px); }
  to { opacity: 1; transform: none; }
}
@keyframes fade {
  from { opacity: 0; }
  to { opacity: 1; }
}
@keyframes pageIn {
  from { opacity: 0; transform: translateY(8px); }
  to { opacity: 1; transform: none; }
}
@keyframes pop {
  from { opacity: 0; transform: translateY(10px) scale(0.98); }
  to { opacity: 1; transform: none; }
}
@keyframes slideDown {
  from { opacity: 0; transform: translateY(-100%); }
  to { opacity: 1; transform: none; }
}
@keyframes toastIn {
  from { opacity: 0; transform: translateY(12px); }
  to { opacity: 1; transform: none; }
}

.toolbar-copy {
  flex: 1;
  margin: 0;
  color: var(--muted);
  font-size: 13px;
  align-self: center;
}
.modal-card.wide { width: min(920px, 100%); }
.autocut-list {
  display: grid;
  gap: 12px;
}
.autocut-card {
  display: grid;
  grid-template-columns: 72px 1fr auto;
  gap: 14px;
  align-items: center;
  background: var(--surface);
  border: 1px solid var(--line);
  border-radius: var(--radius);
  padding: 14px;
  box-shadow: var(--shadow);
}
.autocut-card img,
.autocut-thumb {
  width: 72px;
  height: 72px;
  border-radius: 12px;
  object-fit: cover;
  background: #f3f4f6;
}
.autocut-thumb {
  display: grid;
  place-items: center;
  color: var(--muted);
  font-size: 12px;
  font-weight: 650;
}
.autocut-card h4 { margin: 0 0 4px; font-size: 16px; letter-spacing: -0.02em; }
.autocut-card p { margin: 0; color: var(--muted); font-size: 13px; }
#logo-preview {
  margin: 0 0 14px;
}
#logo-preview img {
  width: 72px;
  height: 72px;
  object-fit: cover;
  border-radius: 16px;
  border: 1px solid var(--line);
  background: #fff;
}
.crop-stage {
  position: relative;
  width: 100%;
  display: flex;
  justify-content: center;
  align-items: center;
  min-height: 180px;
  max-height: min(70dvh, 640px);
  background: #111;
  border-radius: 12px;
  overflow: hidden;
  user-select: none;
  touch-action: none;
}
.crop-stage img {
  display: block;
  width: auto;
  height: auto;
  max-width: 100%;
  max-height: min(70dvh, 640px);
  object-fit: contain;
  pointer-events: none;
}
.crop-box {
  position: absolute;
  border: 2px solid #fff;
  box-shadow: 0 0 0 9999px rgba(0, 0, 0, 0.45);
  cursor: move;
}
.crop-handle {
  position: absolute;
  width: 14px;
  height: 14px;
  background: #fff;
  border-radius: 3px;
}
.crop-handle.nw { left: -7px; top: -7px; cursor: nwse-resize; }
.crop-handle.ne { right: -7px; top: -7px; cursor: nesw-resize; }
.crop-handle.sw { left: -7px; bottom: -7px; cursor: nesw-resize; }
.crop-handle.se { right: -7px; bottom: -7px; cursor: nwse-resize; }
.file-btn {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  width: 100%;
  border: 1px dashed var(--line-strong);
  border-radius: 11px;
  padding: 14px;
  background: #fafbfc;
  color: var(--text);
  font-weight: 650;
  cursor: pointer;
}
.file-btn input { display: none; }

.theme-toggle {
  background: var(--surface);
  color: var(--text);
  border: 1px solid var(--line);
  border-radius: 999px;
  padding: 8px 12px;
  font-size: 13px;
  font-weight: 650;
  min-height: 42px;
}
.theme-pills {
  display: flex;
  flex-wrap: wrap;
  gap: 8px;
  margin: 8px 0 16px;
}
.theme-pills button {
  background: var(--surface);
  color: var(--text);
  border: 1px solid var(--line-strong);
  border-radius: 999px;
  padding: 10px 14px;
  font-weight: 650;
}
.theme-pills button.active {
  background: var(--text);
  color: var(--surface);
  border-color: var(--text);
}
.darkness-pills {
  margin: 0 0 10px;
}
.darkness-pills button {
  min-height: 42px;
  touch-action: manipulation;
}
.settings-grid {
  display: grid;
  grid-template-columns: repeat(2, minmax(0, 1fr));
  gap: 16px;
}
.settings-grid .panel { padding: 22px; animation: none; }
.brand-preview,
.catalog-preview {
  display: grid;
  place-items: center;
  min-height: 96px;
  margin: 0 0 12px;
  border: 1px dashed var(--line-strong);
  border-radius: 12px;
  background: var(--bg);
  color: var(--muted);
}
.brand-preview img,
.catalog-preview img {
  max-width: 100%;
  max-height: 140px;
  object-fit: contain;
  border-radius: 10px;
}
.catalog-grid {
  display: grid;
  grid-template-columns: repeat(auto-fill, minmax(220px, 1fr));
  gap: 14px;
}
.catalog-card {
  display: flex;
  flex-direction: column;
  background: var(--surface);
  border: 1px solid var(--line);
  border-radius: var(--radius);
  overflow: hidden;
  box-shadow: var(--shadow);
}
.catalog-card img,
.catalog-thumb {
  width: 100%;
  height: 140px;
  object-fit: cover;
  background: var(--bg);
}
.catalog-thumb {
  display: grid;
  place-items: center;
  color: var(--muted);
  font-size: 12px;
  font-weight: 650;
}
.catalog-card-body { padding: 12px 14px 8px; }
.catalog-cat {
  display: inline-block;
  font-size: 11px;
  font-weight: 700;
  letter-spacing: 0.04em;
  text-transform: uppercase;
  color: var(--blue);
  margin-bottom: 4px;
}
.catalog-card h4 { margin: 0 0 4px; font-size: 15px; }
.catalog-card p { margin: 0; color: var(--muted); font-size: 13px; }
.catalog-details { margin-top: 6px !important; }
.catalog-price {
  display: flex;
  flex-wrap: wrap;
  align-items: baseline;
  gap: 8px;
  margin: 8px 0 2px;
}
.catalog-price-old {
  color: var(--muted);
  text-decoration: line-through;
  font-size: 13px;
}
.catalog-price-new {
  color: var(--text);
  font-weight: 700;
  font-size: 15px;
  letter-spacing: -0.02em;
}
.form-row {
  display: grid;
  grid-template-columns: 1fr 1fr;
  gap: 12px;
}
@media (max-width: 640px) {
  .form-row { grid-template-columns: 1fr; }
}
.catalog-card .row-actions { padding: 0 12px 12px; }
.setup-list { display: grid; gap: 10px; margin-top: 12px; }
.setup-card {
  display: grid;
  gap: 8px;
  border: 1px solid var(--line);
  border-radius: 12px;
  padding: 14px;
  background: var(--surface);
}
.setup-card h4 { margin: 4px 0; font-size: 15px; }
.setup-url { margin: 0; font-size: 13px; overflow-wrap: anywhere; }
.setup-url a { color: var(--accent, #2563eb); }
.setup-card .row-actions { margin-top: 4px; }
.token-cards { display: grid; gap: 10px; padding: 12px; }
.token-card {
  border: 1px solid var(--line);
  border-radius: 12px;
  padding: 14px;
  background: var(--surface);
}
.token-card .token-key {
  display: block;
  overflow-wrap: anywhere;
  white-space: normal;
}
.token-card .row-actions {
  margin-top: 12px;
}
.token-card .row-actions button,
.device-card button {
  min-height: 44px;
  min-width: 72px;
  touch-action: manipulation;
}
.token-card-head {
  display: flex;
  justify-content: space-between;
  gap: 8px;
  align-items: center;
  margin-bottom: 8px;
}
.token-meta, .muted { color: var(--muted); font-size: 13px; margin: 8px 0 0; }
.row-actions.wrap { flex-wrap: wrap; }
.device-list { display: grid; gap: 10px; margin: 12px 0; }
.device-card {
  display: flex;
  gap: 12px;
  align-items: flex-start;
  justify-content: space-between;
  border: 1px solid var(--line);
  border-radius: 12px;
  padding: 12px;
  background: var(--bg);
}
.device-card p { margin: 4px 0 0; color: var(--muted); font-size: 13px; }
.device-card button {
  flex: 0 0 auto;
  min-height: 40px;
  background: var(--surface);
  color: var(--danger);
  border: 1px solid #f0b8b4;
  border-radius: 10px;
  padding: 8px 12px;
  font-weight: 650;
}
textarea {
  width: 100%;
  min-height: 88px;
  resize: vertical;
  background: var(--surface);
  color: var(--text);
  border: 1px solid var(--line-strong);
  border-radius: 11px;
  padding: 12px 13px;
}

html[data-theme="dark"],
html[data-theme="dark"] body {
  color-scheme: dark;
}
html[data-theme="dark"] {
  --bg: #121418;
  --surface: #1c1f26;
  --text: #f3f5f7;
  --muted: #a8b0bb;
  --line: #2c323c;
  --line-strong: #3a4250;
  --blue: #6ea8ff;
  --blue-soft: rgba(110, 168, 255, 0.16);
  --ok: #5ddea0;
  --ok-bg: rgba(15, 123, 58, 0.22);
  --danger: #ff8d85;
  --danger-bg: rgba(180, 35, 24, 0.22);
  --shadow: 0 1px 2px rgba(0, 0, 0, 0.24), 0 16px 40px rgba(0, 0, 0, 0.28);
  --shadow-lg: 0 24px 60px rgba(0, 0, 0, 0.4);
}
@media (prefers-color-scheme: dark) {
  html:not([data-theme="light"]) {
    --bg: #121418;
    --surface: #1c1f26;
    --text: #f3f5f7;
    --muted: #a8b0bb;
    --line: #2c323c;
    --line-strong: #3a4250;
    --blue: #6ea8ff;
    --blue-soft: rgba(110, 168, 255, 0.16);
    --ok: #5ddea0;
    --ok-bg: rgba(15, 123, 58, 0.22);
    --danger: #ff8d85;
    --danger-bg: rgba(180, 35, 24, 0.22);
    --shadow: 0 1px 2px rgba(0, 0, 0, 0.24), 0 16px 40px rgba(0, 0, 0, 0.28);
    --shadow-lg: 0 24px 60px rgba(0, 0, 0, 0.4);
  }
}
html[data-theme="dark"] .login-view {
  background: linear-gradient(180deg, #171a20 0%, var(--bg) 50%);
}
html[data-theme="dark"] .login-aside {
  background: linear-gradient(165deg, #1c1f26 0%, #171a20 58%, var(--blue-soft) 100%);
}
html[data-theme="dark"] input,
html[data-theme="dark"] select,
html[data-theme="dark"] textarea,
html[data-theme="dark"] .menu,
html[data-theme="dark"] .chip,
html[data-theme="dark"] .row-actions button,
html[data-theme="dark"] .file-btn,
html[data-theme="dark"] th,
html[data-theme="dark"] tbody tr:hover,
html[data-theme="dark"] .token-key,
html[data-theme="dark"] .install-banner {
  background: var(--surface);
  color: var(--text);
}
html[data-theme="dark"] .sidebar nav button:hover,
html[data-theme="dark"] .ghost:hover {
  background: #262b34 !important;
  color: var(--text) !important;
}
html[data-theme="dark"] .sidebar nav button.active,
html[data-theme="dark"] button[type="submit"],
html[data-theme="dark"] .toolbar button,
html[data-theme="dark"] .modal-actions button:not(.ghost) {
  background: #f3f5f7;
  color: #111;
}
html[data-theme="dark"] button[type="submit"]:hover,
html[data-theme="dark"] .toolbar button:hover,
html[data-theme="dark"] .modal-actions button:not(.ghost):hover {
  background: #fff;
}
html[data-theme="dark"] .crop-stage { background: #000; }

@media (prefers-color-scheme: dark) {
  html:not([data-theme="light"]) .login-view {
    background: linear-gradient(180deg, #171a20 0%, var(--bg) 50%);
  }
  html:not([data-theme="light"]) .login-aside {
    background: linear-gradient(165deg, #1c1f26 0%, #171a20 58%, var(--blue-soft) 100%);
  }
  html:not([data-theme="light"]) input,
  html:not([data-theme="light"]) select,
  html:not([data-theme="light"]) textarea,
  html:not([data-theme="light"]) .menu,
  html:not([data-theme="light"]) .chip,
  html:not([data-theme="light"]) .row-actions button,
  html:not([data-theme="light"]) .file-btn,
  html:not([data-theme="light"]) th,
  html:not([data-theme="light"]) tbody tr:hover,
  html:not([data-theme="light"]) .token-key,
  html:not([data-theme="light"]) .install-banner {
    background: var(--surface);
    color: var(--text);
  }
  html:not([data-theme="light"]) .sidebar nav button:hover,
  html:not([data-theme="light"]) .ghost:hover {
    background: #262b34 !important;
    color: var(--text) !important;
  }
  html:not([data-theme="light"]) .sidebar nav button.active,
  html:not([data-theme="light"]) button[type="submit"],
  html:not([data-theme="light"]) .toolbar button,
  html:not([data-theme="light"]) .modal-actions button:not(.ghost) {
    background: #f3f5f7;
    color: #111;
  }
  html:not([data-theme="light"]) .crop-stage { background: #000; }
}

@media (max-width: 980px) {
  .login-shell { grid-template-columns: 1fr; }
  .login-aside { display: none; }
  .stats { grid-template-columns: 1fr 1fr; }
  .app-view {
    display: block;
    width: 100%;
  }
  .app-view > main {
    width: 100%;
    min-height: 100dvh;
  }
  .settings-grid { grid-template-columns: 1fr; }
  .app-view > .sidebar {
    position: fixed;
    inset: 0 auto 0 0;
    width: min(280px, 86vw);
    max-width: min(280px, 86vw);
    transform: translateX(-110%);
    transition: transform 0.28s var(--ease);
    z-index: 30;
    box-shadow: var(--shadow-lg);
    pointer-events: none;
    min-height: 0;
    max-height: 100dvh;
    height: 100dvh;
  }
  .sidebar.open {
    transform: none;
    pointer-events: auto;
  }
  .sidebar-backdrop {
    pointer-events: none;
  }
  .sidebar-backdrop.visible {
    display: block;
    position: fixed;
    inset: 0;
    background: rgba(17, 17, 17, 0.28);
    z-index: 25;
    animation: fade 0.2s ease;
    pointer-events: auto;
  }
  .menu { display: inline-flex; }
  main { padding: 16px; padding-bottom: max(24px, env(safe-area-inset-bottom)); }
  .topbar {
    position: sticky;
    top: 0;
    z-index: 15;
    background: color-mix(in srgb, var(--bg) 88%, transparent);
    backdrop-filter: blur(10px);
    -webkit-backdrop-filter: blur(10px);
    padding: 8px 4px 12px;
    margin: -8px -4px 14px;
    border-bottom: 1px solid transparent;
  }
  .topbar-copy { flex: 1 1 100%; order: 2; }
  .menu { order: 0; flex-shrink: 0; }
  #theme-toggle { order: 1; margin-left: auto; }
  .install-banner {
    flex-wrap: wrap;
    gap: 10px;
    padding: 12px 16px;
  }
  .install-banner .install-actions { width: 100%; justify-content: flex-end; }
}
@media (max-width: 720px) {
  .stats { grid-template-columns: 1fr 1fr; }
  .toolbar { flex-direction: column; align-items: stretch; }
  .toolbar button { width: 100%; min-height: 44px; }
  .login-card { padding: 28px 22px; }
  .chip { display: none; }
  .topbar h2 { font-size: 22px; }
  .modal { padding: 8px; align-items: end; }
  .modal-card,
  .modal-card.wide {
    width: 100%;
    max-height: min(92dvh, 100%);
    border-radius: 18px 18px 12px 12px;
    padding: 18px 16px calc(16px + env(safe-area-inset-bottom));
  }
  .modal-actions { flex-direction: column-reverse; }
  .modal-actions button { width: 100%; min-height: 44px; }
  .darkness-pills { margin-bottom: 12px; }
  .darkness-pills button { min-height: 44px; }
  .row-actions button { min-height: 40px; }
  .autocut-card {
    grid-template-columns: 64px 1fr;
  }
  .autocut-card .row-actions {
    grid-column: 1 / -1;
  }
  .catalog-grid { grid-template-columns: 1fr; }
  .device-card { flex-direction: column; }
  .device-card button { width: 100%; }
  .toast {
    left: 12px;
    right: 12px;
    bottom: max(12px, env(safe-area-inset-bottom));
  }
  table { min-width: 0; }
  .table-wrap table th,
  .table-wrap table td { white-space: normal; word-break: break-word; }
  .table-wrap table td.token-key { max-width: none; }
}
@media (max-width: 420px) {
  .stats { grid-template-columns: 1fr; }
}

@media (prefers-reduced-motion: reduce) {
  *, *::before, *::after {
    animation: none !important;
    transition: none !important;
  }
}
