:root {
  --primary: #300060;
  --primary-dark: #200050;
  --primary-soft: #f0e8f8;
  --accent: #c08010;
  --accent-dark: #a06010;
  --accent-soft: #fdf6e8;
  --ink: #100020;
  --muted: #6b5878;
  --bg: #f7f5fa;
  --surface: #ffffff;
  --surface-2: #f5f2f8;
  --border: #e8e0f0;
  --border-strong: #d4c8e0;
  --success: #0b6b16;
  --success-bg: #e8f5e9;
  --error: #b91c1c;
  --error-bg: #fef2f2;
  --shadow-sm: 0 1px 3px rgba(0, 0, 0, 0.06);
  --shadow-md: 0 4px 12px rgba(0, 0, 0, 0.08);
  --radius-sm: 8px;
  --radius-md: 12px;
  --radius-lg: 16px;
  --footer-h: 68px;
  --font-display: "Segoe UI", system-ui, -apple-system, sans-serif;
  --font-body: "Segoe UI", system-ui, -apple-system, sans-serif;
  --transition: 150ms ease;
}

* { box-sizing: border-box; }

html, body {
  margin: 0;
  padding: 0;
  height: 100%;
}

body {
  font-family: var(--font-body);
  background: var(--bg);
  color: var(--ink);
  font-size: 15px;
  line-height: 1.5;
  -webkit-font-smoothing: antialiased;
}

a {
  color: var(--primary);
  text-decoration: none;
}

a:hover { text-decoration: underline; }

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

/* ── App shell ── */

.app-shell {
  min-height: 100vh;
  height: 100dvh;
  width: 100%;
  display: flex;
  flex-direction: column;
  background: var(--bg);
  overflow: hidden;
}

.top-header {
  background: var(--surface);
  border-bottom: 1px solid var(--border);
  padding: 12px 16px 10px;
  position: sticky;
  top: 0;
  z-index: 20;
  box-shadow: var(--shadow-sm);
}

.header-bar {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 8px 12px;
}

.header-user {
  display: flex;
  align-items: center;
  gap: 6px;
  font-size: 0.85rem;
  color: var(--muted);
  text-decoration: none;
  flex-shrink: 0;
  max-width: 45%;
}

.header-user[hidden] { display: none; }

.header-user span {
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}

.top-header h1 {
  margin: 0;
  font-family: var(--font-display);
  font-size: 1.2rem;
  font-weight: 700;
  color: var(--ink);
  flex: 1;
  min-width: 0;
}

.top-tabs {
  display: flex;
  gap: 4px;
  overflow-x: auto;
  margin-top: 10px;
  padding-bottom: 2px;
  -webkit-overflow-scrolling: touch;
}

.top-tabs[hidden] { display: none; }

.top-tab {
  flex: 1 1 0;
  text-align: center;
  padding: 8px 10px;
  border-radius: var(--radius-sm);
  font-family: var(--font-display);
  font-size: 0.72rem;
  font-weight: 600;
  color: var(--muted);
  text-decoration: none;
  white-space: nowrap;
  transition: background var(--transition), color var(--transition);
  min-height: 36px;
  display: flex;
  align-items: center;
  justify-content: center;
}

.top-tab:hover {
  background: var(--surface-2);
  text-decoration: none;
  color: var(--ink);
}

.top-tab.is-active {
  background: var(--primary);
  color: #fff;
}

.top-tab.is-active:hover { color: #fff; }

.page-body {
  flex: 1;
  min-height: 0;
  display: flex;
  flex-direction: column;
  padding: 16px 16px calc(var(--footer-h) + 16px);
}

#page-content {
  flex: 1;
  min-height: 0;
  overflow: auto;
  animation: fadeIn 0.2s ease;
}

@keyframes fadeIn {
  from { opacity: 0; transform: translateY(4px); }
  to { opacity: 1; transform: translateY(0); }
}

#page-content:has(.table-wrap) {
  overflow: hidden;
  display: flex;
  flex-direction: column;
}

/* ── Bottom nav ── */

.bottom-nav {
  position: fixed;
  left: 0;
  right: 0;
  bottom: 0;
  height: var(--footer-h);
  background: var(--surface);
  border-top: 1px solid var(--border);
  display: grid;
  grid-template-columns: repeat(4, 1fr);
  z-index: 30;
  box-shadow: 0 -2px 8px rgba(0, 0, 0, 0.04);
}

.nav-item {
  color: var(--muted);
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  gap: 3px;
  font-size: 0.68rem;
  font-weight: 600;
  text-decoration: none;
  transition: color var(--transition);
  min-height: 44px;
  position: relative;
}

.nav-item i { font-size: 1.15rem; }

.nav-item.is-active {
  color: var(--primary);
}

.nav-item.is-active::before {
  content: "";
  position: absolute;
  top: 0;
  left: 25%;
  right: 25%;
  height: 2px;
  background: var(--primary);
  border-radius: 0 0 2px 2px;
}

.nav-item:hover {
  color: var(--primary);
  text-decoration: none;
}

/* ── Auth pages ── */

.auth-page {
  min-height: 100vh;
  display: grid;
  place-items: center;
  padding: 24px 16px;
  background: var(--bg);
}

.auth-card {
  width: min(420px, 100%);
  background: var(--surface);
  border-radius: var(--radius-lg);
  padding: 32px 28px;
  box-shadow: var(--shadow-md);
  border: 1px solid var(--border);
}

.auth-card-secondary {
  margin-top: 16px;
}

.auth-card-secondary h2 {
  font-size: 1.1rem;
  margin-bottom: 16px;
}

.auth-logo {
  margin: 0 auto 12px;
  display: flex;
  justify-content: center;
  align-items: center;
}

.auth-logo img {
  max-width: min(220px, 100%);
  width: auto;
  height: auto;
  display: block;
}

.auth-tagline {
  text-align: center;
  margin: -8px 0 20px;
  font-size: 0.85rem;
  color: var(--muted);
}

.auth-card h2 {
  font-family: var(--font-display);
  text-align: center;
  margin: 0 0 24px;
  font-size: 1.35rem;
  font-weight: 700;
  color: var(--ink);
}

/* ── Forms ── */

.field { margin-bottom: 16px; }

.field label {
  display: block;
  font-size: 0.82rem;
  font-weight: 600;
  margin-bottom: 6px;
  color: var(--ink);
}

.required-mark { color: var(--error); }

.field-input-wrap {
  position: relative;
  display: flex;
  align-items: center;
}

.field input,
.field select {
  width: 100%;
  padding: 12px 14px;
  border: 1px solid var(--border-strong);
  border-radius: var(--radius-sm);
  font-size: 1rem;
  font-family: var(--font-body);
  background: var(--surface);
  color: var(--ink);
  transition: border-color var(--transition), box-shadow var(--transition);
}

.field-input-wrap input { padding-right: 44px; }

.field input[readonly] {
  background: var(--surface-2);
  color: var(--muted);
  cursor: default;
}

.field input:focus,
.field select:focus {
  outline: none;
  border-color: var(--primary);
  box-shadow: 0 0 0 3px rgba(48, 0, 96, 0.12);
}

.password-toggle {
  position: absolute;
  right: 4px;
  background: none;
  border: none;
  color: var(--muted);
  cursor: pointer;
  padding: 8px 10px;
  font-size: 0.9rem;
  border-radius: var(--radius-sm);
}

.password-toggle:hover { color: var(--primary); }

.btn {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  gap: 8px;
  width: 100%;
  border: none;
  border-radius: var(--radius-sm);
  padding: 13px 16px;
  font-size: 0.95rem;
  font-weight: 600;
  font-family: var(--font-body);
  cursor: pointer;
  background: var(--accent);
  color: #fff;
  transition: background var(--transition), transform var(--transition);
}

.btn:hover { background: var(--accent-dark); }

.btn:active { transform: scale(0.98); }

.btn:disabled {
  opacity: 0.65;
  cursor: not-allowed;
  transform: none;
}

.btn-secondary {
  background: var(--surface);
  color: var(--primary);
  border: 1px solid var(--border-strong);
}

.btn-secondary:hover {
  background: var(--accent-soft);
  border-color: var(--accent);
}

.btn-sm {
  width: auto;
  padding: 8px 14px;
  font-size: 0.82rem;
}

.auth-alt {
  margin-top: 20px;
  text-align: center;
  font-size: 0.88rem;
  color: var(--muted);
}

/* ── Cards ── */

.card {
  background: var(--surface);
  border-radius: var(--radius-md);
  box-shadow: var(--shadow-sm);
  border: 1px solid var(--border);
  padding: 16px;
  margin-bottom: 12px;
}

.card h2,
.card h3 {
  font-family: var(--font-display);
  margin: 0 0 12px;
  color: var(--ink);
  font-size: 1rem;
  font-weight: 700;
}

.section-title {
  font-family: var(--font-display);
  font-size: 0.78rem;
  font-weight: 700;
  text-transform: uppercase;
  letter-spacing: 0.06em;
  color: var(--muted);
  margin: 16px 0 8px;
}

.section-title:first-child { margin-top: 0; }

/* ── Dashboard hero ── */

.hero-card {
  background: var(--primary);
  border: none;
  color: #fff;
  padding: 24px 20px;
  margin-bottom: 12px;
  border-radius: var(--radius-md);
  box-shadow: var(--shadow-md);
}

.hero-card .hero-label {
  font-size: 0.78rem;
  font-weight: 600;
  text-transform: uppercase;
  letter-spacing: 0.08em;
  opacity: 0.85;
  margin-bottom: 4px;
}

.hero-card .hero-number {
  font-family: var(--font-display);
  font-size: 3.5rem;
  font-weight: 800;
  line-height: 1;
  margin-bottom: 12px;
}

.hero-card .hero-meta {
  display: flex;
  flex-wrap: wrap;
  gap: 8px 16px;
  font-size: 0.88rem;
  opacity: 0.92;
}

.hero-meta-item {
  display: flex;
  align-items: center;
  gap: 6px;
}

.stat-grid {
  display: grid;
  grid-template-columns: 1fr 1fr 1fr;
  gap: 10px;
}

.stat-grid-2 {
  display: grid;
  grid-template-columns: 1fr 1fr;
  gap: 10px;
}

.stat-card {
  background: var(--surface);
  border-radius: var(--radius-md);
  box-shadow: var(--shadow-sm);
  border: 1px solid var(--border);
  padding: 14px 12px;
}

.stat-card h3 {
  margin: 0 0 6px;
  font-size: 0.72rem;
  font-weight: 600;
  color: var(--muted);
  text-transform: uppercase;
  letter-spacing: 0.04em;
}

.stat-card .value {
  font-family: var(--font-display);
  font-size: 1.4rem;
  font-weight: 700;
  color: var(--ink);
}

.stat-card .value-text {
  font-size: 0.88rem;
  font-weight: 600;
  line-height: 1.3;
}

.lucky-grid {
  display: grid;
  grid-template-columns: 1fr;
  gap: 8px;
}

.lucky-item {
  display: flex;
  justify-content: space-between;
  align-items: center;
  padding: 10px 12px;
  background: var(--surface-2);
  border-radius: var(--radius-sm);
  font-size: 0.88rem;
}

.lucky-item span:first-child { color: var(--muted); font-weight: 500; }
.lucky-item span:last-child { font-weight: 600; color: var(--ink); }

.traits { display: grid; gap: 8px; }

.trait {
  background: var(--surface-2);
  border-radius: var(--radius-sm);
  padding: 12px 14px;
  font-size: 0.88rem;
  line-height: 1.5;
  border-left: 3px solid var(--accent);
}

.info-row {
  display: flex;
  justify-content: space-between;
  gap: 12px;
  padding: 8px 0;
  font-size: 0.9rem;
  border-bottom: 1px solid var(--border);
}

.info-row:last-child { border-bottom: none; }

.info-row .value {
  color: var(--accent-dark);
  font-weight: 600;
  text-align: right;
}

/* ── Tools hub ── */

.tool-grid {
  display: grid;
  gap: 10px;
}

.tool-link {
  display: flex;
  align-items: center;
  gap: 14px;
  padding: 16px;
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: var(--radius-md);
  text-decoration: none;
  color: var(--ink);
  transition: border-color var(--transition), box-shadow var(--transition);
}

.tool-link:hover {
  border-color: var(--primary);
  box-shadow: var(--shadow-sm);
  text-decoration: none;
}

.tool-link-icon {
  width: 44px;
  height: 44px;
  border-radius: var(--radius-sm);
  background: var(--accent-soft);
  color: var(--accent);
  display: grid;
  place-items: center;
  font-size: 1.1rem;
  flex-shrink: 0;
}

.tool-link-text h3 {
  margin: 0 0 2px;
  font-family: var(--font-display);
  font-size: 0.95rem;
  font-weight: 700;
  color: var(--ink);
}

.tool-link-text p {
  margin: 0;
  font-size: 0.8rem;
  color: var(--muted);
}

/* ── Calculator results ── */

.results-card {
  margin-top: 12px;
  animation: fadeIn 0.25s ease;
}

.results-card .number-breakdown {
  margin-top: 4px;
  padding: 0 0 4px;
}

.number-breakdown {
  display: flex;
  align-items: center;
  gap: 8px;
  font-family: var(--font-display);
  font-size: 0.88rem;
  font-weight: 600;
  color: var(--muted);
}

.number-breakdown .arrow {
  color: var(--muted);
  font-weight: 400;
}

/* ── Charts ── */

.chart-card { margin-bottom: 14px; }

.chart-meta { margin-bottom: 12px; }

.chart-grid {
  display: grid;
  grid-template-columns: repeat(3, 1fr);
  gap: 6px;
  width: min(100%, 300px);
  margin: 0 auto;
}

.chart-cell {
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: var(--radius-sm);
  min-height: 72px;
  display: grid;
  place-items: center;
  font-family: var(--font-display);
  font-weight: 700;
  color: var(--accent);
  font-size: 1.35rem;
  box-shadow: var(--shadow-sm);
}

.chart-cell.is-empty {
  color: var(--border-strong);
  font-weight: 400;
}

/* ── Tables ── */

.table-toolbar {
  display: flex;
  justify-content: space-between;
  align-items: center;
  gap: 8px;
  margin-bottom: 8px;
}

.table-subject {
  display: flex;
  align-items: center;
  gap: 6px;
  font-size: 0.78rem;
  font-weight: 500;
  min-width: 0;
  flex: 1;
}

.table-subject-name {
  font-weight: 600;
  color: var(--ink);
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}

.table-subject-sep { color: var(--muted); }

.table-subject-dob { color: var(--muted); }

.table-subject-edit {
  color: var(--accent-dark);
  font-size: 0.75rem;
  font-weight: 600;
  padding: 4px 8px;
  border-radius: var(--radius-sm);
  background: var(--accent-soft);
  text-decoration: none;
  white-space: nowrap;
  flex-shrink: 0;
}

.table-subject-edit:hover { text-decoration: none; background: var(--accent-soft); }

.data-table {
  width: 100%;
  border-collapse: separate;
  border-spacing: 0;
  background: var(--surface);
  font-size: 0.82rem;
}

.data-table th {
  position: sticky;
  top: 0;
  z-index: 2;
  background: var(--primary);
  color: #fff;
  text-align: left;
  padding: 10px 8px;
  font-weight: 600;
  font-size: 0.75rem;
  white-space: nowrap;
}

.data-table td {
  padding: 10px 8px;
  border-bottom: 1px solid var(--border);
}

.data-table tr:nth-child(even) td { background: var(--surface-2); }

.data-table tr.is-current td {
  background: var(--primary-soft) !important;
  font-weight: 600;
}

.data-table td.num {
  color: var(--accent-dark);
  font-weight: 700;
  text-align: center;
}

.table-note {
  margin: 0;
  padding: 10px 12px;
  font-size: 0.75rem;
  color: var(--muted);
  background: var(--surface-2);
  border-top: 1px solid var(--border);
}

.table-wrap {
  overflow: auto;
  width: 100%;
  max-height: 100%;
  border-radius: var(--radius-md);
  box-shadow: var(--shadow-sm);
  background: var(--surface);
  border: 1px solid var(--border);
  -webkit-overflow-scrolling: touch;
}

#page-content:has(.table-wrap) .table-wrap {
  flex: 1;
  min-height: 0;
}

/* ── Profile actions ── */

.profile-actions {
  display: grid;
  gap: 8px;
  margin-top: 16px;
  padding-top: 16px;
  border-top: 1px solid var(--border);
}

.profile-note {
  font-size: 0.82rem;
  color: var(--muted);
  background: var(--surface-2);
  border-radius: var(--radius-sm);
  padding: 10px 12px;
  margin-bottom: 16px;
  line-height: 1.45;
}

.logout-link {
  display: flex;
  align-items: center;
  justify-content: center;
  gap: 8px;
  padding: 12px;
  color: var(--error);
  font-weight: 600;
  font-size: 0.9rem;
  border: 1px solid #fecaca;
  border-radius: var(--radius-sm);
  background: var(--error-bg);
  text-decoration: none;
  transition: background var(--transition);
}

.logout-link:hover {
  background: #fee2e2;
  text-decoration: none;
}

/* ── Flash & toast ── */

.flash {
  padding: 12px 14px;
  border-radius: var(--radius-sm);
  margin-bottom: 12px;
  font-size: 0.88rem;
  font-weight: 500;
}

.flash-error {
  background: var(--error-bg);
  color: var(--error);
  border: 1px solid #fecaca;
}

.flash-success {
  background: var(--success-bg);
  color: var(--success);
  border: 1px solid #bbf7d0;
}

.toast-container {
  position: fixed;
  top: 16px;
  left: 50%;
  transform: translateX(-50%);
  z-index: 100;
  display: flex;
  flex-direction: column;
  gap: 8px;
  pointer-events: none;
  width: min(360px, calc(100% - 32px));
}

.toast {
  padding: 12px 16px;
  border-radius: var(--radius-sm);
  font-size: 0.88rem;
  font-weight: 500;
  box-shadow: var(--shadow-md);
  animation: toastIn 0.25s ease, toastOut 0.25s ease 2.75s forwards;
  pointer-events: auto;
}

.toast-success {
  background: var(--success);
  color: #fff;
}

.toast-error {
  background: var(--error);
  color: #fff;
}

@keyframes toastIn {
  from { opacity: 0; transform: translateY(-12px); }
  to { opacity: 1; transform: translateY(0); }
}

@keyframes toastOut {
  from { opacity: 1; }
  to { opacity: 0; }
}

/* ── Loading & skeleton ── */

.page-loading {
  display: grid;
  place-items: center;
  gap: 12px;
  padding: 48px 12px;
  color: var(--muted);
  font-size: 0.88rem;
}

.spinner {
  width: 32px;
  height: 32px;
  border: 3px solid var(--border);
  border-top-color: var(--primary);
  border-radius: 50%;
  animation: spin 0.7s linear infinite;
}

@keyframes spin { to { transform: rotate(360deg); } }

.skeleton-page { display: grid; gap: 12px; }

.skeleton {
  background: var(--surface-2);
  border-radius: var(--radius-sm);
}

.skeleton-hero { height: 140px; border-radius: var(--radius-md); }

.skeleton-grid {
  display: grid;
  grid-template-columns: repeat(3, 1fr);
  gap: 10px;
}

.skeleton-stat { height: 72px; }

.skeleton-card { height: 120px; border-radius: var(--radius-md); }

/* ── Responsive ── */

@media (min-width: 640px) {
  .top-header { padding: 14px 20px 12px; }
  .top-header h1 { font-size: 1.35rem; }
  .top-tab { font-size: 0.78rem; padding: 9px 12px; }
  .page-body { padding: 20px 20px calc(var(--footer-h) + 20px); }
  .stat-grid { grid-template-columns: repeat(3, 1fr); }
  .lucky-grid { grid-template-columns: repeat(3, 1fr); }
  .chart-grid { width: min(100%, 340px); }
  .chart-cell { min-height: 80px; font-size: 1.45rem; }
}

@media (min-width: 960px) {
  .page-body { padding: 24px 28px calc(var(--footer-h) + 24px); max-width: 960px; margin: 0 auto; width: 100%; }
  .auth-card { padding: 40px 36px; }
  .chart-grid { width: min(100%, 380px); }
  .data-table { font-size: 0.88rem; }
}

@media (prefers-color-scheme: dark) {
  :root {
    --ink: #f0e8f8;
    --muted: #a89bb8;
    --bg: #0a0014;
    --surface: #140028;
    --surface-2: #1c0038;
    --border: #2a1048;
    --border-strong: #3a1860;
    --primary: #5a0090;
    --primary-dark: #400070;
    --primary-soft: #1e0038;
    --accent: #d09010;
    --accent-dark: #b07010;
    --accent-soft: #2a1808;
    --error-bg: #2a1515;
    --success-bg: #152a18;
  }

  .top-header,
  .bottom-nav {
    background: var(--surface);
    border-color: var(--border);
  }

  .auth-page {
    background: var(--bg);
  }

  .hero-card {
    background: var(--primary);
  }

  .data-table th { background: var(--primary-dark); }
}
