/* Theme tokens: only `html` + `html[data-theme="light"]`. Do not use :root for
   colors — :root always matches <html> and ties with attribute selectors, which
   breaks light mode in several browsers. */

.foot-version {
  margin-left: 0.35em;
  font-weight: 600;
  font-variant-numeric: tabular-nums;
  color: var(--muted);
}

html {
  color-scheme: dark;
  --canvas: #0d1117;
  --bg: var(--canvas);
  --bg-elevated: #151b24;
  --card: #1a222e;
  --border: #2d3a4d;
  --border-input: #3d4d64;
  --input-bg: #121820;
  --text: #f0f4fa;
  --muted: #94a3b8;
  --accent: #5b8cff;
  --accent-hover: #89b4ff;
  --accent-text: #0a0f18;
  --danger: #fb7185;
  --ok: #4ade80;
  --sidebar-bg: #111827;
  --sidebar-fg: #f8fafc;
  --sidebar-fg-muted: rgba(248, 250, 252, 0.65);
  --sidebar-hover: rgba(255, 255, 255, 0.07);
  --sidebar-active: rgba(255, 255, 255, 0.11);
  --sidebar-border: rgba(255, 255, 255, 0.08);
  --sidebar-accent-bar: #5b8cff;
  --row-hover: rgba(91, 140, 255, 0.1);
  --focus-ring: rgba(91, 140, 255, 0.45);
  --dialog-overlay: rgba(2, 6, 12, 0.72);
  --toggle-off: #475569;
  --shadow: 0 10px 40px rgba(0, 0, 0, 0.45);
  --radius: 10px;
  --radius-lg: 14px;
  --table-header: #161f2e;
}

html[data-theme="light"] {
  color-scheme: light;
  --canvas: #eceff3;
  --bg: var(--canvas);
  --bg-elevated: #ffffff;
  --card: #ffffff;
  --border: #e2e7ee;
  --border-input: #d1d9e6;
  --input-bg: #fafbfc;
  --text: #1a1d26;
  --muted: #5c6578;
  --accent: #4d64d8;
  --accent-hover: #3d52c4;
  --accent-text: #ffffff;
  --danger: #dc2626;
  --ok: #16a34a;
  --sidebar-bg: #4d64d8;
  --sidebar-fg: #ffffff;
  --sidebar-fg-muted: rgba(255, 255, 255, 0.78);
  --sidebar-hover: rgba(255, 255, 255, 0.14);
  --sidebar-active: rgba(0, 0, 0, 0.12);
  --sidebar-border: rgba(255, 255, 255, 0.18);
  --sidebar-accent-bar: #ffffff;
  --row-hover: rgba(77, 100, 216, 0.08);
  --focus-ring: rgba(77, 100, 216, 0.4);
  --dialog-overlay: rgba(26, 29, 38, 0.48);
  --toggle-off: #94a3b8;
  --shadow: 0 8px 28px rgba(26, 29, 38, 0.09);
  --radius: 10px;
  --radius-lg: 14px;
  --table-header: #f1f4f9;
}

* {
  box-sizing: border-box;
}

html {
  height: 100%;
}

body {
  margin: 0;
  min-height: 100%;
  font-family: Inter, system-ui, -apple-system, "Segoe UI", Roboto, sans-serif;
  font-size: 15px;
  background: var(--bg);
  color: var(--text);
  line-height: 1.55;
  -webkit-font-smoothing: antialiased;
}

/* Auth layout (login) */
body.layout-auth {
  display: flex;
  flex-direction: column;
  min-height: 100vh;
  background:
    radial-gradient(ellipse 100% 90% at 50% -30%, rgba(56, 189, 248, 0.14), transparent 55%),
    var(--bg);
}

html[data-theme="light"] body.layout-auth {
  background:
    radial-gradient(ellipse 100% 90% at 50% -25%, rgba(77, 100, 216, 0.12), transparent 50%),
    var(--bg);
}

body.layout-auth .auth-main {
  flex: 1;
  display: flex;
  align-items: center;
  justify-content: center;
  padding: 1.75rem 1rem;
}

body.layout-auth .auth-stack {
  width: 100%;
  max-width: 400px;
  background: var(--card);
  border: 1px solid var(--border);
  border-radius: var(--radius-lg);
  box-shadow: var(--shadow);
  padding: 2rem 1.75rem 1.75rem;
}

body.layout-auth .auth-brand {
  text-align: center;
  margin: 0 0 1.5rem;
}

body.layout-auth .auth-brand a {
  font-size: 1.4rem;
  font-weight: 700;
  letter-spacing: -0.02em;
  color: var(--text);
  text-decoration: none;
}

body.layout-auth .auth-brand .tag {
  display: block;
  font-size: 0.8125rem;
  font-weight: 500;
  color: var(--muted);
  margin-top: 0.35rem;
}

body.layout-auth .foot {
  margin-top: auto;
}

body.layout-auth .auth-form {
  margin: 0;
  padding: 0;
  border: none;
  background: transparent;
  box-shadow: none;
}

body.layout-auth .auth-form button[type="submit"] {
  width: 100%;
  margin-top: 1rem;
  padding: 0.65rem 1rem;
  font-size: 0.95rem;
}

/* App shell + sidebar */
body.layout-app {
  min-height: 100vh;
}

.app-shell {
  display: flex;
  min-height: 100vh;
}

.sidebar {
  width: 260px;
  flex-shrink: 0;
  background: var(--sidebar-bg);
  border-right: 1px solid var(--sidebar-border);
  display: flex;
  flex-direction: column;
  padding: 1.1rem 0.85rem 1rem;
  color: var(--sidebar-fg);
}

.sidebar .brand {
  display: block;
  padding: 0.45rem 0.65rem 0.85rem;
  color: var(--sidebar-fg);
  text-decoration: none;
  font-weight: 700;
  font-size: 1.15rem;
  letter-spacing: -0.03em;
  margin-bottom: 0.15rem;
  border-radius: var(--radius);
}

.sidebar .brand:hover {
  background: var(--sidebar-hover);
}

.sidebar-user {
  display: flex;
  align-items: center;
  gap: 0.65rem;
  padding: 0.65rem 0.6rem 1rem;
  margin-bottom: 0.25rem;
  border-bottom: 1px solid var(--sidebar-border);
}

.sidebar-user-avatar {
  width: 2.5rem;
  height: 2.5rem;
  border-radius: 50%;
  background: var(--sidebar-hover);
  color: var(--sidebar-fg);
  display: flex;
  align-items: center;
  justify-content: center;
  font-size: 0.8rem;
  font-weight: 700;
  flex-shrink: 0;
}

.sidebar-user-name {
  font-size: 0.875rem;
  font-weight: 600;
  color: var(--sidebar-fg);
  line-height: 1.25;
}

.sidebar-user-meta {
  font-size: 0.75rem;
  color: var(--sidebar-fg-muted);
  line-height: 1.3;
  word-break: break-all;
}

.sidebar-nav {
  display: flex;
  flex-direction: column;
  gap: 0.15rem;
  flex: 1;
  margin-top: 0.25rem;
}

.sidebar-nav a {
  display: block;
  padding: 0.55rem 0.7rem;
  border-radius: var(--radius);
  color: var(--sidebar-fg-muted);
  text-decoration: none;
  font-size: 0.875rem;
  font-weight: 500;
  transition: background 0.12s ease, color 0.12s ease;
}

.sidebar-nav a:hover {
  color: var(--sidebar-fg);
  background: var(--sidebar-hover);
}

.sidebar-nav a.is-active {
  color: var(--sidebar-fg);
  background: var(--sidebar-active);
  box-shadow: inset 3px 0 0 var(--sidebar-accent-bar);
}

.sidebar-footer {
  margin-top: auto;
  padding-top: 0.85rem;
  border-top: 1px solid var(--sidebar-border);
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 0.65rem;
}

/* Lucide theme control (sidebar) */
.theme-toggle-icon {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  width: 2.65rem;
  height: 2.65rem;
  padding: 0;
  margin: 0;
  border: 1px solid var(--sidebar-border);
  border-radius: 50%;
  background: var(--sidebar-hover);
  color: var(--sidebar-fg);
  cursor: pointer;
  transition: background 0.12s ease, transform 0.08s ease;
}

.theme-toggle-icon:hover {
  background: var(--sidebar-active);
}

.theme-toggle-icon:focus-visible {
  outline: 2px solid var(--focus-ring);
  outline-offset: 2px;
}

.theme-ico-wrap {
  display: none;
  align-items: center;
  justify-content: center;
  line-height: 0;
}

.theme-ico-wrap svg {
  width: 1.2rem;
  height: 1.2rem;
  stroke: currentColor;
}

html[data-theme="dark"] .theme-ico-wrap-sun {
  display: flex;
}

html[data-theme="dark"] .theme-ico-wrap-moon {
  display: none;
}

html[data-theme="light"] .theme-ico-wrap-sun {
  display: none;
}

html[data-theme="light"] .theme-ico-wrap-moon {
  display: flex;
}

.theme-toggle-icon--auth {
  border-color: var(--border);
  background: var(--bg-elevated);
  color: var(--text);
}

.theme-toggle-icon--auth:hover {
  background: var(--row-hover);
}

.sidebar .sign-out {
  text-align: center;
  font-size: 0.8125rem;
}

.sidebar .sign-out a {
  color: var(--sidebar-fg-muted);
  text-decoration: none;
  font-weight: 500;
}

.sidebar .sign-out a:hover {
  color: var(--sidebar-fg);
  text-decoration: underline;
}

.app-main {
  flex: 1;
  min-width: 0;
  display: flex;
  flex-direction: column;
  background: var(--canvas);
}

.content-wrap {
  flex: 1;
  padding: 1.35rem 1.5rem 2.5rem;
  max-width: 1200px;
  width: 100%;
  margin: 0 auto;
}

.content-wrap a:not(.btn):not(.tile) {
  color: var(--accent);
  text-decoration: none;
  font-weight: 500;
}

.content-wrap a:not(.btn):not(.tile):hover {
  text-decoration: underline;
  color: var(--accent-hover, var(--accent));
}

.content-wrap label:not(.toggle),
.modal-inner label:not(.toggle),
.auth-stack label:not(.toggle) {
  display: block;
  margin-bottom: 0.75rem;
  color: var(--muted);
  font-size: 0.8125rem;
  font-weight: 500;
}

/* Legacy wrap inside content */
.wrap {
  max-width: 1200px;
  margin: 0 auto;
  padding: 0;
}

.page-lede {
  color: var(--muted);
  margin: 0 0 1.35rem;
  font-size: 0.9375rem;
  line-height: 1.5;
  max-width: 42rem;
}

.page-actions {
  display: flex;
  flex-wrap: wrap;
  gap: 0.5rem;
  align-items: center;
  margin-bottom: 1rem;
}

.page-actions a {
  margin-top: 0;
}

.subject-tabs {
  display: flex;
  flex-wrap: wrap;
  gap: 0.35rem;
  margin-bottom: 1rem;
}

.subject-tabs a {
  display: inline-block;
  padding: 0.35rem 0.75rem;
  border-radius: 999px;
  border: 1px solid var(--border);
  color: var(--muted);
  font-size: 0.8125rem;
  font-weight: 600;
  text-decoration: none;
}

.subject-tabs a.is-active {
  background: var(--accent-soft);
  border-color: var(--accent);
  color: var(--accent);
}

.password-field {
  position: relative;
  display: block;
}

.password-field input {
  padding-right: 4.25rem;
}

.password-field__toggle {
  position: absolute;
  right: 0.35rem;
  top: 50%;
  transform: translateY(-50%);
  margin: 0;
  padding: 0.35rem 0.5rem;
  border: none;
  border-radius: 6px;
  background: transparent;
  color: var(--accent);
  font-size: 0.8rem;
  font-weight: 600;
  cursor: pointer;
}

.password-field__toggle:hover {
  background: var(--accent-soft);
}

h1 {
  font-size: 1.5rem;
  margin: 0 0 1rem;
  font-weight: 700;
  letter-spacing: -0.02em;
  line-height: 1.25;
}

h2.section-title {
  font-size: 1rem;
  font-weight: 600;
  margin: 1.5rem 0 0.75rem;
  color: var(--text);
}

.card {
  background: var(--card);
  padding: 1.35rem 1.4rem;
  border-radius: var(--radius-lg);
  border: 1px solid var(--border);
  max-width: 100%;
  box-shadow: var(--shadow);
}

.card.narrow {
  max-width: 420px;
}

.card.wide {
  max-width: 720px;
}

input,
select,
textarea {
  width: 100%;
  padding: 0.55rem 0.7rem;
  border-radius: var(--radius);
  border: 1px solid var(--border-input);
  background: var(--input-bg);
  color: var(--text);
  font-size: 0.9375rem;
}

input:focus-visible,
select:focus-visible,
textarea:focus-visible,
button:focus-visible {
  outline: 2px solid var(--focus-ring);
  outline-offset: 2px;
}

button:hover,
.btn:hover {
  filter: brightness(1.06);
}

button:active,
.btn:active {
  filter: brightness(0.96);
}

button,
.btn {
  display: inline-block;
  margin-top: 0.5rem;
  padding: 0.55rem 1.1rem;
  background: var(--accent);
  color: var(--accent-text);
  border: none;
  border-radius: var(--radius);
  font-weight: 600;
  font-size: 0.9rem;
  cursor: pointer;
  text-decoration: none;
  text-align: center;
  transition: filter 0.12s ease, transform 0.08s ease;
}

.btn-secondary {
  background: var(--border-input);
  color: var(--text);
}

.btn-ghost {
  background: transparent;
  color: var(--muted);
  border: 1px solid var(--border);
}

.btn-ghost:hover {
  color: var(--text);
  border-color: var(--muted);
}

.btn-sm {
  padding: 0.35rem 0.65rem;
  font-size: 0.8125rem;
  margin-top: 0;
}

.error {
  color: var(--danger);
}

.ok {
  color: var(--ok);
}

/* Form grid */
.form-grid {
  display: grid;
  grid-template-columns: repeat(2, minmax(0, 1fr));
  gap: 0.85rem 1.1rem;
}

.form-grid label:not(.toggle) {
  margin-bottom: 0;
}

.form-grid .span-2 {
  grid-column: 1 / -1;
}

@media (max-width: 540px) {
  .form-grid {
    grid-template-columns: 1fr;
  }

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

/* Toggle switch (checkbox) */
.toggle {
  position: relative;
  display: flex;
  align-items: center;
  gap: 0.75rem;
  cursor: pointer;
  margin-bottom: 0;
  padding: 0.35rem 0;
  color: var(--text);
  font-size: 0.9rem;
}

.toggle input {
  position: absolute;
  opacity: 0;
  width: 0;
  height: 0;
}

.toggle-track {
  position: relative;
  width: 44px;
  height: 24px;
  flex-shrink: 0;
  background: var(--toggle-off);
  border-radius: 999px;
  transition: background 0.2s;
}

.toggle-track::after {
  content: "";
  position: absolute;
  top: 2px;
  left: 2px;
  width: 20px;
  height: 20px;
  border-radius: 50%;
  background: #fff;
  box-shadow: 0 1px 3px rgba(0, 0, 0, 0.2);
  transition: transform 0.2s;
}

.toggle input:checked + .toggle-track {
  background: var(--accent);
}

.toggle input:checked + .toggle-track::after {
  transform: translateX(20px);
}

.toggle input:focus-visible + .toggle-track {
  outline: 2px solid var(--focus-ring);
  outline-offset: 2px;
}

.toggle-label {
  color: var(--muted);
  font-size: 0.8125rem;
}

/* Tables */
.table-scroll {
  overflow: auto;
  max-height: min(72vh, 600px);
  border-radius: var(--radius-lg);
  border: 1px solid var(--border);
  background: var(--card);
  box-shadow: 0 1px 0 rgba(0, 0, 0, 0.04);
}

table.data-table {
  width: 100%;
  min-width: 100%;
  border-collapse: collapse;
  table-layout: auto;
}

table.data-table th,
table.data-table td {
  padding: 0.7rem 0.95rem;
  text-align: left;
  vertical-align: middle;
  border-bottom: 1px solid var(--border);
  white-space: normal;
  word-break: break-word;
}

table.data-table td.wrap-cell {
  max-width: 22rem;
}

table.data-table thead th {
  position: sticky;
  top: 0;
  z-index: 1;
  background: var(--table-header);
  color: var(--muted);
  font-size: 0.6875rem;
  text-transform: uppercase;
  letter-spacing: 0.06em;
  font-weight: 600;
  white-space: nowrap;
}

table.data-table tbody tr:last-child td {
  border-bottom: none;
}

table.data-table tbody tr:hover td {
  background: var(--row-hover);
}

table.data-table tbody td.row-actions {
  white-space: normal;
  overflow: visible;
  text-align: right;
  vertical-align: middle;
}

table.data-table tbody td.joined-cell {
  white-space: nowrap;
  font-variant-numeric: tabular-nums;
  font-size: 0.875rem;
  color: var(--text);
}

.row-actions {
  white-space: normal;
}

.row-actions a {
  margin-right: 0.65rem;
  font-weight: 500;
}

.tiles {
  list-style: none;
  padding: 0;
  margin: 1rem 0 0;
  display: grid;
  gap: 0.75rem;
  grid-template-columns: repeat(auto-fill, minmax(200px, 1fr));
}

.tile {
  display: block;
  padding: 1rem;
  background: var(--card);
  border-radius: 12px;
  border: 1px solid var(--border);
  color: var(--text);
  text-decoration: none;
  font-weight: 500;
  box-shadow: var(--shadow);
}

.tile:hover {
  border-color: var(--accent);
}

.foot {
  padding: 1rem;
  text-align: center;
  color: var(--muted);
  font-size: 0.8rem;
}

.foot-theme-row {
  margin: 0 0 0.75rem;
  display: flex;
  justify-content: center;
}

.muted {
  color: var(--muted);
}

/* Dialog / modal */
dialog.modal {
  border: none;
  border-radius: var(--radius-lg);
  padding: 0;
  margin: auto;
  max-width: min(520px, calc(100vw - 1.5rem));
  width: 100%;
  max-height: min(calc(100vh - 2rem), 900px);
  background: var(--card);
  color: var(--text);
  box-shadow: var(--shadow);
}

dialog.modal.wide {
  max-width: min(700px, calc(100vw - 1.5rem));
}

dialog.modal::backdrop {
  background: var(--dialog-overlay);
  backdrop-filter: blur(3px);
}

.modal-inner {
  padding: 1.35rem 1.5rem 1.5rem;
  max-height: min(calc(100vh - 3rem), 860px);
  overflow-y: auto;
}

.modal-header {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 1rem;
  margin-bottom: 1rem;
  padding-bottom: 0.75rem;
  border-bottom: 1px solid var(--border);
}

.modal-header h2 {
  margin: 0;
  font-size: 1.1rem;
  font-weight: 700;
}

.modal-actions {
  display: flex;
  flex-wrap: wrap;
  gap: 0.5rem;
  margin-top: 1rem;
}

.modal-actions button,
.modal-actions .btn {
  margin-top: 0;
}

.dialog-close {
  margin: 0;
  padding: 0.35rem 0.6rem;
  background: transparent;
  color: var(--muted);
  border: none;
  font-size: 1.25rem;
  line-height: 1;
  cursor: pointer;
  border-radius: 6px;
}

.dialog-close:hover {
  color: var(--text);
  background: var(--row-hover);
}

.inline-form {
  display: inline;
}

.inline-form button {
  margin-top: 0;
}

@media (max-width: 768px) {
  .app-shell {
    flex-direction: column;
  }

  .sidebar {
    width: 100%;
    flex-direction: row;
    flex-wrap: nowrap;
    align-items: center;
    gap: 0.5rem;
    border-right: none;
    border-bottom: 1px solid var(--border);
    padding: 0.65rem 0.75rem;
    overflow-x: auto;
    -webkit-overflow-scrolling: touch;
  }

  .sidebar .brand {
    flex-shrink: 0;
    margin-bottom: 0;
    white-space: nowrap;
  }

  .sidebar-nav {
    flex-direction: row;
    flex-wrap: nowrap;
    flex: 1 1 auto;
    gap: 0.2rem;
    margin-top: 0;
    min-width: 0;
  }

  .sidebar-nav a {
    flex-shrink: 0;
    white-space: nowrap;
    font-size: 0.8125rem;
    padding: 0.45rem 0.6rem;
  }

  .sidebar-nav a.is-active {
    box-shadow: inset 0 -2px 0 var(--accent);
  }

  .sidebar-footer {
    flex-direction: row;
    flex-shrink: 0;
    margin-top: 0;
    padding-top: 0;
    border-top: none;
    gap: 0.35rem;
    align-items: center;
  }

  .sidebar-footer .theme-toggle-icon {
    width: 2.4rem;
    height: 2.4rem;
    padding: 0;
  }
}
