/* ===================== Design tokens ===================== */
:root {
  --primary: #6366f1;
  --primary-dark: #4f46e5;
  --primary-light: #eef2ff;
  --success: #16a34a;
  --success-light: #dcfce7;
  --warning: #d97706;
  --warning-light: #fef3c7;
  --danger: #dc2626;
  --danger-light: #fee2e2;

  --bg: #f4f5f9;
  --surface: #ffffff;
  --surface-alt: #f8f9fc;
  --border: #e5e7eb;
  --text: #111827;
  --text-muted: #6b7280;
  --text-faint: #9ca3af;

  --radius-sm: 8px;
  --radius: 12px;
  --radius-lg: 18px;
  --shadow: 0 1px 3px rgba(17, 24, 39, 0.06), 0 1px 2px rgba(17, 24, 39, 0.04);
  --shadow-lg: 0 12px 32px rgba(17, 24, 39, 0.12);

  --sidebar-w: 240px;
  --font-size-base: 16px;
}

:root[data-theme="dark"] {
  --bg: #0f1115;
  --surface: #171922;
  --surface-alt: #1d2028;
  --border: #2a2e3a;
  --text: #f3f4f6;
  --text-muted: #9ca3af;
  --text-faint: #6b7280;
  --primary-light: #1e1f3a;
  --success-light: #113321;
  --warning-light: #3a2a0d;
  --danger-light: #3a1414;
  --shadow: 0 1px 3px rgba(0, 0, 0, 0.4);
  --shadow-lg: 0 12px 32px rgba(0, 0, 0, 0.5);
}

@media (prefers-color-scheme: dark) {
  :root:not([data-theme="light"]) {
    --bg: #0f1115;
    --surface: #171922;
    --surface-alt: #1d2028;
    --border: #2a2e3a;
    --text: #f3f4f6;
    --text-muted: #9ca3af;
    --text-faint: #6b7280;
    --primary-light: #1e1f3a;
    --success-light: #113321;
    --warning-light: #3a2a0d;
    --danger-light: #3a1414;
  }
}

* { box-sizing: border-box; }
html, body { margin: 0; padding: 0; }
body {
  background: var(--bg);
  color: var(--text);
  font-family: "Noto Sans Thai", "Inter", system-ui, -apple-system, "Segoe UI", sans-serif;
  font-size: var(--font-size-base);
  line-height: 1.5;
  -webkit-font-smoothing: antialiased;
}
a { color: var(--primary); text-decoration: none; }
a:hover { text-decoration: underline; }
h1, h2, h3, h4 { margin: 0 0 .5rem; line-height: 1.3; }
p { margin: 0 0 .75rem; }

/* ===================== App shell ===================== */
.app-shell { display: flex; min-height: 100vh; }

.sidebar {
  width: var(--sidebar-w);
  flex-shrink: 0;
  background: var(--surface);
  border-right: 1px solid var(--border);
  display: flex;
  flex-direction: column;
  position: fixed;
  top: 0; bottom: 0; left: 0;
  z-index: 40;
  transition: transform .2s ease;
}
.sidebar-brand {
  display: flex; align-items: center; gap: .6rem;
  padding: 1.25rem 1.25rem 1rem;
  font-weight: 700; font-size: 1.1rem;
}
.sidebar-brand .logo-dot {
  width: 32px; height: 32px; border-radius: 9px;
  background: linear-gradient(135deg, var(--primary), #a855f7);
  display: flex; align-items: center; justify-content: center;
  color: #fff; font-weight: 800; font-size: .95rem;
}
.sidebar-nav { flex: 1; overflow-y: auto; padding: .5rem .75rem; }
.sidebar-nav a {
  display: flex; align-items: center; gap: .65rem;
  padding: .6rem .75rem; margin-bottom: .15rem;
  border-radius: var(--radius-sm);
  color: var(--text-muted); font-weight: 500; font-size: .92rem;
  text-decoration: none;
}
.sidebar-nav a:hover { background: var(--surface-alt); color: var(--text); }
.sidebar-nav a.active { background: var(--primary-light); color: var(--primary-dark); }
.sidebar-nav .nav-icon { width: 18px; text-align: center; flex-shrink: 0; }
.sidebar-footer {
  padding: 1rem; border-top: 1px solid var(--border);
  font-size: .82rem; color: var(--text-muted);
}
.sidebar-footer .user-row { display: flex; align-items: center; gap: .5rem; margin-bottom: .6rem; }
.sidebar-footer .avatar {
  width: 30px; height: 30px; border-radius: 50%;
  background: var(--primary-light); color: var(--primary-dark);
  display: flex; align-items: center; justify-content: center; font-weight: 700; font-size: .85rem;
  flex-shrink: 0;
}
.sidebar-footer .footer-actions { display: grid; grid-template-columns: 1fr 1fr; gap: .4rem; }
.sidebar-footer .footer-actions > * { min-width: 0; }
.sidebar-footer .footer-actions button { width: 100%; box-sizing: border-box; }

.main {
  flex: 1;
  margin-left: var(--sidebar-w);
  min-width: 0;
}
.topbar {
  position: sticky; top: 0; z-index: 30;
  display: flex; align-items: center; justify-content: space-between;
  gap: 1rem;
  padding: .85rem 1.5rem;
  background: color-mix(in srgb, var(--bg) 85%, transparent);
  backdrop-filter: blur(8px);
  border-bottom: 1px solid var(--border);
}
.topbar h1 { font-size: 1.4rem; margin: 0; }
.menu-toggle {
  display: none; background: none; border: none; font-size: 1.3rem; cursor: pointer; color: var(--text);
}
.content { padding: 1.5rem; max-width: 1600px; }

/* ===================== Buttons ===================== */
.btn {
  display: inline-flex; align-items: center; justify-content: center; gap: .4rem;
  padding: .55rem 1rem;
  border-radius: var(--radius-sm);
  border: 1px solid transparent;
  font-size: .9rem; font-weight: 600;
  cursor: pointer;
  text-decoration: none;
  transition: filter .12s ease, background .12s ease;
  line-height: 1.2;
}
.btn:hover { text-decoration: none; filter: brightness(0.97); }
.btn-primary { background: var(--primary); color: #fff; }
.btn-primary:hover { background: var(--primary-dark); }
.btn-outline { background: transparent; border-color: var(--border); color: var(--text); }
.btn-outline:hover { background: var(--surface-alt); }
.btn-danger { background: var(--danger); color: #fff; }
.btn-danger-outline { background: transparent; border-color: var(--danger); color: var(--danger); }
.btn-success { background: var(--success); color: #fff; }
.btn-sm { padding: .35rem .7rem; font-size: .8rem; }
.btn-icon { padding: .45rem; width: 34px; height: 34px; }
.btn-block { width: 100%; }
.btn[disabled] { opacity: .5; cursor: not-allowed; }

/* ===================== Cards ===================== */
.card {
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  box-shadow: var(--shadow);
}
.card-pad { padding: 1.25rem; }
.card-header {
  display: flex; align-items: center; justify-content: space-between; gap: 1rem;
  padding: 1rem 1.25rem; border-bottom: 1px solid var(--border);
}
.card-header h2, .card-header h3 { margin: 0; font-size: 1.15rem; }
.grid { display: grid; gap: 1rem; }
.grid-2 { grid-template-columns: repeat(2, 1fr); }
.grid-3 { grid-template-columns: repeat(3, 1fr); }
.grid-4 { grid-template-columns: repeat(4, 1fr); }
@media (max-width: 900px) {
  .grid-2, .grid-3, .grid-4 { grid-template-columns: 1fr; }
}

.stat-card { padding: 1.1rem 1.25rem; }
.stat-card .stat-label { font-size: .8rem; color: var(--text-muted); margin-bottom: .3rem; }
.stat-card .stat-value { font-size: 1.5rem; font-weight: 700; }
.stat-card .stat-sub { font-size: .78rem; color: var(--text-faint); margin-top: .2rem; }

/* ===================== Forms ===================== */
label, .form-label { display: block; font-size: 1rem; font-weight: 700; margin-bottom: .4rem; color: var(--text-muted); }
.field { margin-bottom: 1rem; }
.input, input[type=text], input[type=email], input[type=password], input[type=number],
input[type=date], input[type=color], select, textarea {
  width: 100%;
  padding: .6rem .75rem;
  border: 1px solid var(--border);
  border-radius: var(--radius-sm);
  background: var(--surface);
  color: var(--text);
  font-size: .92rem;
  font-family: inherit;
}
input:focus, select:focus, textarea:focus, .input:focus {
  outline: none; border-color: var(--primary);
  box-shadow: 0 0 0 3px var(--primary-light);
}
input[type=color] { padding: .25rem; height: 42px; cursor: pointer; }
.checkbox-row { display: flex; align-items: center; gap: .5rem; }
.checkbox-row input { width: auto; }
.checkbox-pills { display: flex; flex-wrap: wrap; gap: .5rem; }
.checkbox-pill {
  display: flex; align-items: center; gap: .4rem;
  border: 1px solid var(--border); border-radius: 999px;
  padding: .4rem .75rem; font-size: .85rem; cursor: pointer;
}
.checkbox-pill input { width: auto; }
.help-text { font-size: .78rem; color: var(--text-faint); margin-top: .3rem; }
.error-text { font-size: .8rem; color: var(--danger); margin-top: .3rem; }
.form-row { display: flex; gap: 1rem; flex-wrap: wrap; }
.form-row > .field { flex: 1; min-width: 180px; }

/* ===================== Tables ===================== */
table.data-table { width: 100%; border-collapse: collapse; font-size: .88rem; }
table.data-table th {
  text-align: left; font-size: 1rem; font-weight: 700;
  color: var(--text); padding: .7rem 1rem; border-bottom: 1px solid var(--border);
  white-space: nowrap;
}
table.data-table td { padding: .8rem 1rem; border-bottom: 1px solid var(--border); vertical-align: middle; }
table.data-table tr:last-child td { border-bottom: none; }
table.data-table tr.row-child { background: var(--surface-alt); }
table.data-table td.actions, table.data-table th.actions { white-space: nowrap; }
table.data-table td.actions .btn, table.data-table td.actions form { display: inline-flex; vertical-align: middle; }
.table-scroll { overflow-x: auto; }

/* ===================== Badges / pills ===================== */
.badge {
  display: inline-flex; align-items: center; gap: .3rem;
  padding: .25rem .6rem; border-radius: 999px;
  font-size: .74rem; font-weight: 700;
}
.badge-success { background: var(--success-light); color: var(--success); }
.badge-warning { background: var(--warning-light); color: var(--warning); }
.badge-danger { background: var(--danger-light); color: var(--danger); }
.badge-neutral { background: var(--surface-alt); color: var(--text-muted); border: 1px solid var(--border); }
.member-tag {
  display: inline-flex; align-items: center; gap: .35rem;
  padding: .2rem .55rem; border-radius: 999px; font-size: .76rem; font-weight: 600;
  background: var(--surface-alt); border: 1px solid var(--border);
}
.member-dot { width: 8px; height: 8px; border-radius: 50%; flex-shrink: 0; }

/* ===================== Progress ===================== */
.progress-track { height: 8px; border-radius: 999px; background: var(--surface-alt); overflow: hidden; }
.progress-fill { height: 100%; border-radius: 999px; background: var(--primary); transition: width .3s ease; }

/* ===================== Alerts / messages ===================== */
.alert { padding: .8rem 1rem; border-radius: var(--radius-sm); font-size: .88rem; margin-bottom: .75rem; border: 1px solid transparent; }
.alert-success { background: var(--success-light); color: var(--success); border-color: color-mix(in srgb, var(--success) 30%, transparent); }
.alert-error { background: var(--danger-light); color: var(--danger); border-color: color-mix(in srgb, var(--danger) 30%, transparent); }
.alert-info { background: var(--primary-light); color: var(--primary-dark); border-color: color-mix(in srgb, var(--primary) 30%, transparent); }
.alert-warning { background: var(--warning-light); color: var(--warning); border-color: color-mix(in srgb, var(--warning) 30%, transparent); }

/* ===================== Modal ===================== */
.modal-backdrop {
  display: none; position: fixed; inset: 0; background: rgba(15, 17, 21, .5);
  align-items: center; justify-content: center; z-index: 100; padding: 1rem;
}
.modal-backdrop.open { display: flex; }
.modal-box {
  background: var(--surface); border-radius: var(--radius-lg); box-shadow: var(--shadow-lg);
  width: 100%; max-width: 420px; padding: 1.5rem;
}
.modal-box h3 { margin-bottom: .5rem; }
.modal-actions { display: flex; justify-content: flex-end; gap: .5rem; margin-top: 1.25rem; }

/* ===================== Misc ===================== */
.empty-state { text-align: center; padding: 3rem 1rem; color: var(--text-muted); }
.empty-state .empty-icon { font-size: 2.5rem; margin-bottom: .5rem; }
.text-muted { color: var(--text-muted); }
.text-danger { color: var(--danger); }
.text-success { color: var(--success); }
.text-right { text-align: right; }
.flex { display: flex; align-items: center; }
.flex-between { display: flex; align-items: center; justify-content: space-between; }
.gap-2 { gap: .5rem; }
.gap-3 { gap: .75rem; }
.mt-2 { margin-top: .5rem; }
.mt-3 { margin-top: .75rem; }
.mt-4 { margin-top: 1rem; }
.mb-3 { margin-bottom: .75rem; }
.filter-bar {
  display: flex; flex-wrap: wrap; gap: .75rem; align-items: flex-end;
  padding: 1rem 1.25rem; margin-bottom: 1.25rem;
}
.filter-bar .field { margin-bottom: 0; min-width: 160px; }
.section-title { font-size: 1.1rem; font-weight: 700; margin: 1.5rem 0 .75rem; }

/* ===================== Login page ===================== */
.login-wrap {
  min-height: 100vh; display: flex; align-items: center; justify-content: center;
  background: linear-gradient(135deg, var(--primary-light), var(--bg));
  padding: 1rem;
}
.login-card { width: 100%; max-width: 380px; padding: 2rem; }
.login-card .logo-dot { margin: 0 auto 1rem; }
.login-card h1 { text-align: center; font-size: 1.3rem; }
.login-card .sub { text-align: center; color: var(--text-muted); font-size: .88rem; margin-bottom: 1.5rem; }

/* ===================== Responsive ===================== */
@media (max-width: 860px) {
  .sidebar { transform: translateX(-100%); }
  .sidebar.open { transform: translateX(0); }
  .main { margin-left: 0; }
  .menu-toggle { display: inline-flex; }
  .content { padding: 1rem; }
}
