:root {
  --bg: #f5f5f2;
  --surface: #ffffff;
  --text: #1c1c1a;
  --muted: #6b6b66;
  --border: #e2e2dc;
  --accent: #2f5bd3;
  --accent-text: #ffffff;
  --danger: #c0392b;
  --ok-bg: #e3f4e8;
  --ok-text: #1d6b3a;
  --warn-bg: #fdf0dc;
  --warn-text: #8a5300;
  --off-bg: #ececea;
  --off-text: #55554f;
  --radius: 10px;
  color-scheme: light;
}

@media (prefers-color-scheme: dark) {
  :root {
    --bg: #161615;
    --surface: #1f1f1d;
    --text: #ecece8;
    --muted: #9b9b94;
    --border: #33332f;
    --accent: #7096ff;
    --accent-text: #0d1330;
    --danger: #ff7a6b;
    --ok-bg: #173622;
    --ok-text: #7fd89d;
    --warn-bg: #3a2a10;
    --warn-text: #f3be6b;
    --off-bg: #2c2c29;
    --off-text: #aaaaa3;
    color-scheme: dark;
  }
}

* { box-sizing: border-box; }

body {
  margin: 0;
  font: 15px/1.5 system-ui, -apple-system, "Segoe UI", sans-serif;
  background: var(--bg);
  color: var(--text);
}

[hidden] { display: none !important; }

h1, h2 { margin: 0 0 12px; line-height: 1.2; }
h1 { font-size: 1.5rem; }
h2 { font-size: 1.15rem; }
p { margin: 0; }
a { color: var(--accent); }
.muted { color: var(--muted); }
.error { color: var(--danger); }

.card {
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  padding: 20px;
}

/* Controls */
input, select, button, .button {
  font: inherit;
  color: inherit;
}

input:not([type="color"]):not([type="checkbox"]), select {
  width: 100%;
  padding: 8px 10px;
  border: 1px solid var(--border);
  border-radius: 8px;
  background: var(--bg);
}

input[type="color"] {
  width: 100%;
  height: 38px;
  padding: 2px;
  border: 1px solid var(--border);
  border-radius: 8px;
  background: var(--bg);
}

label {
  display: flex;
  flex-direction: column;
  gap: 4px;
  font-weight: 500;
}

label.checkbox {
  flex-direction: row;
  align-items: center;
  gap: 8px;
}

button, .button {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  padding: 8px 14px;
  border-radius: 8px;
  border: 1px solid var(--border);
  background: var(--surface);
  cursor: pointer;
  text-decoration: none;
  white-space: nowrap;
}

button.primary {
  background: var(--accent);
  border-color: var(--accent);
  color: var(--accent-text);
  font-weight: 600;
}

button.ghost { background: transparent; }
button.danger { color: var(--danger); border-color: currentColor; background: transparent; }
button:disabled { opacity: .6; cursor: default; }

:focus-visible { outline: 2px solid var(--accent); outline-offset: 2px; }

/* Auth */
.auth {
  min-height: 100vh;
  display: grid;
  place-items: center;
  padding: 16px;
}

.auth-card {
  width: 100%;
  max-width: 360px;
  display: flex;
  flex-direction: column;
  gap: 14px;
}

/* App shell */
.topbar {
  display: flex;
  justify-content: space-between;
  align-items: center;
  gap: 12px;
  padding: 12px 16px;
  border-bottom: 1px solid var(--border);
  background: var(--surface);
}

.brand { font-weight: 700; }
.topbar-right { display: flex; align-items: center; gap: 12px; min-width: 0; }
#user-email { overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }

.layout {
  max-width: 1040px;
  margin: 0 auto;
  padding: 20px 16px 48px;
  display: flex;
  flex-direction: column;
  gap: 20px;
}

/* Forms */
.link-form {
  display: grid;
  grid-template-columns: 1fr 1fr;
  gap: 14px;
}

.span-2 { grid-column: span 2; }

.style-fields {
  display: grid;
  grid-template-columns: repeat(4, 1fr);
  gap: 12px;
  margin: 0;
  padding: 12px;
  border: 1px solid var(--border);
  border-radius: 8px;
}

.style-fields legend { padding: 0 4px; font-weight: 600; }

/* Result */
.result {
  display: flex;
  gap: 20px;
  align-items: center;
  margin-top: 20px;
  padding-top: 20px;
  border-top: 1px solid var(--border);
}

.qr-preview {
  width: 180px;
  height: 180px;
  flex-shrink: 0;
  border-radius: 6px;
  border: 1px solid var(--border);
}

.result-details { display: flex; flex-direction: column; gap: 4px; min-width: 0; }
.short-link-row, .download-row { display: flex; flex-wrap: wrap; gap: 8px; align-items: center; margin-bottom: 8px; }
.short-url { font-weight: 600; overflow-wrap: anywhere; }

/* Table */
.section-head { display: flex; justify-content: space-between; align-items: baseline; }

table { width: 100%; border-collapse: collapse; }
th, td { text-align: left; padding: 10px 8px; border-bottom: 1px solid var(--border); vertical-align: top; }
th { font-size: .8rem; text-transform: uppercase; letter-spacing: .04em; color: var(--muted); font-weight: 600; }
th.num, td.num { text-align: right; font-variant-numeric: tabular-nums; }
tbody tr { cursor: pointer; }
tbody tr:hover, tbody tr:focus-visible { background: var(--bg); }
td.slug { font-family: ui-monospace, SFMono-Regular, Menlo, monospace; }
td.target { max-width: 320px; overflow-wrap: anywhere; }

.badge {
  display: inline-block;
  padding: 2px 8px;
  border-radius: 999px;
  font-size: .8rem;
  font-weight: 600;
  white-space: nowrap;
}
.badge.active { background: var(--ok-bg); color: var(--ok-text); }
.badge.expired_time, .badge.expired_scans { background: var(--warn-bg); color: var(--warn-text); }
.badge.disabled { background: var(--off-bg); color: var(--off-text); }

/* Dialog */
dialog {
  width: min(560px, calc(100vw - 32px));
  max-height: calc(100vh - 32px);
  padding: 20px;
  border: 1px solid var(--border);
  border-radius: var(--radius);
  background: var(--surface);
  color: var(--text);
}
dialog::backdrop { background: rgb(0 0 0 / .45); }

.dialog-head { display: flex; justify-content: space-between; align-items: center; }
.dialog-head h2 { margin: 0; }
.edit-slug { font-family: ui-monospace, SFMono-Regular, Menlo, monospace; }
.edit-preview { display: flex; gap: 16px; align-items: center; }
.edit-preview .qr-preview { width: 120px; height: 120px; }
.dialog-actions { display: flex; gap: 8px; align-items: center; }
.spacer { flex: 1; }

/* Phone widths */
@media (max-width: 640px) {
  .link-form { grid-template-columns: 1fr; }
  .span-2 { grid-column: auto; }
  .style-fields { grid-template-columns: 1fr 1fr; }
  .result, .edit-preview { flex-direction: column; align-items: flex-start; }

  thead { display: none; }
  table, tbody, tr, td { display: block; }
  tr { padding: 10px 0; border-bottom: 1px solid var(--border); }
  td { border: 0; padding: 2px 0; }
  td.num { text-align: left; }
  td[data-label]::before { content: attr(data-label) ": "; color: var(--muted); }
  td.target { max-width: none; }
}
