/* =============================================================
   shell.css — Design Tokens + App Shell (Phase 1 Redesign)
   โหลดหลัง styles.css เพื่อ override token เก่า
   ============================================================= */

/* ----- Design Tokens ----- */
:root {
  --brand:       #2563eb;
  --brand-600:   #2563eb;
  --brand-700:   #1d4ed8;
  --brand-50:    #eff4ff;
  --brand-100:   #dbe6fe;

  --ink:         #0f172a;
  --text:        #334155;
  --muted:       #64748b;
  --faint:       #94a3b8;

  --bg:          #f4f6f9;
  --surface:     #ffffff;
  --surface-2:   #f8fafc;
  --border:      #e6e9ef;
  --border-strong: #d6dbe4;

  --pos:         #16a34a;
  --pos-bg:      #e9f7ef;
  --neg:         #dc2626;
  --neg-bg:      #fdecec;
  --warn:        #d97706;
  --warn-bg:     #fef3e2;
  --info:        #6366f1;
  --info-bg:     #eef0fe;

  /* sidebar */
  --side-bg:       #111a2e;
  --side-bg-2:     #0c1322;
  --side-text:     #aab4c5;
  --side-text-dim: #6b7689;
  --side-active:   #2563eb;

  --radius-xs: 7px;
  --radius-sm: 10px;
  --radius:    14px;
  --radius-lg: 18px;

  --shadow-xs: 0 1px 2px rgba(15,23,42,.05);
  --shadow-sm: 0 1px 3px rgba(15,23,42,.07), 0 1px 2px rgba(15,23,42,.04);
  --shadow:    0 6px 20px -6px rgba(15,23,42,.14);
  --shadow-lg: 0 24px 48px -16px rgba(15,23,42,.28);

  --font:     "IBM Plex Sans Thai", "IBM Plex Sans", system-ui, sans-serif;
  --font-num: "IBM Plex Sans", "IBM Plex Sans Thai", system-ui, sans-serif;

  --pad-y: 12px;
  --gap:   20px;
  --base:  15px;

  /* compat aliases for existing page CSS */
  --primary:       var(--brand);
  --primary-dark:  var(--brand-700);
  --primary-soft:  var(--brand-50);
  --success:       var(--pos);
  --danger:        var(--neg);
  --warning:       var(--warn);
  --heading:       var(--ink);
  --border-soft:   var(--border);
  --surface-soft:  var(--surface-2);
  --radius-xl:     var(--radius-lg);
}

/* ----- Base ----- */
body {
  font-family: var(--font);
  font-size: var(--base);
  color: var(--text);
  background: var(--bg);
  -webkit-font-smoothing: antialiased;
  text-rendering: optimizeLegibility;
  line-height: 1.5;
}

h1,h2,h3,h4 { color: var(--ink); font-weight: 700; margin: 0; letter-spacing: -0.01em; }
a { color: inherit; text-decoration: none; }
.tnum { font-family: var(--font-num); font-variant-numeric: tabular-nums; font-feature-settings: "tnum"; }

/* =============================================================
   App Shell
   ============================================================= */
#root { height: 100%; }

.shell {
  display: grid;
  grid-template-columns: 264px minmax(0, 1fr);
  height: 100vh;
  background: var(--bg);
}

/* ----- Sidebar ----- */
.sidebar {
  background: linear-gradient(176deg, var(--side-bg), var(--side-bg-2));
  color: var(--side-text);
  display: flex;
  flex-direction: column;
  height: 100vh;
  overflow: hidden;
  border-right: 1px solid rgba(255,255,255,.06);
  /* reset old sidebar styles */
  width: auto;
  min-width: 0;
  min-height: 0;
  border-radius: 0;
}

.side-brand {
  display: flex;
  align-items: center;
  gap: 11px;
  padding: 18px 18px 16px;
  flex: none;
}
.side-logo {
  width: 38px; height: 38px;
  border-radius: 11px;
  background: var(--brand);
  display: grid; place-items: center;
  color: #fff; font-weight: 800; font-size: 15px;
  flex: none;
  box-shadow: 0 6px 16px -4px rgba(37,99,235,.55);
}
.side-brand-name { color: #fff; font-weight: 700; font-size: 15px; line-height: 1.15; white-space: nowrap; }
.side-brand-sub { color: var(--side-text-dim); font-size: 11.5px; font-weight: 600; letter-spacing: .04em; text-transform: uppercase; margin-top: 2px; white-space: nowrap; }

.side-scroll { flex: 1; overflow-y: auto; padding: 6px 12px 12px; min-height: 0; }
.side-scroll::-webkit-scrollbar { width: 5px; }
.side-scroll::-webkit-scrollbar-thumb { background: rgba(255,255,255,.1); border-radius: 4px; }

.side-section {
  color: var(--side-text-dim);
  font-size: 10.5px; font-weight: 700;
  letter-spacing: .09em; text-transform: uppercase;
  padding: 16px 12px 7px;
  white-space: nowrap;
}

/* nav items */
.nav-item {
  width: 100%;
  display: flex;
  align-items: center;
  gap: 11px;
  padding: 9px 12px;
  border: 0;
  background: transparent;
  color: var(--side-text);
  border-radius: 10px;
  font-size: 14px;
  font-weight: 600;
  cursor: pointer;
  text-align: left;
  font-family: var(--font);
  transition: background .14s, color .14s;
  position: relative;
  box-shadow: none;
  transform: none;
  min-height: 0;
}
.nav-item:hover { background: rgba(255,255,255,.06) !important; color: #fff !important; box-shadow: none; transform: none; border-color: transparent !important; }
.nav-item.active { background: var(--side-active) !important; color: #fff !important; box-shadow: 0 8px 18px -8px rgba(37,99,235,.5); border-color: transparent !important; }
.nav-item .ico { width: 18px; height: 18px; flex: none; opacity: .9; }
.nav-item > span:not(.nav-badge):not(.chev) { flex: 1; min-width: 0; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.nav-item.active .ico { opacity: 1; }
.nav-item .chev { margin-left: auto; width: 15px; height: 15px; transition: transform .18s; opacity: .6; flex: none; }
.nav-group.open > .nav-item .chev { transform: rotate(90deg); }
.nav-badge { margin-left: auto; background: var(--brand); color:#fff; font-size: 11px; font-weight: 700; padding: 1px 7px; border-radius: 20px; flex: none; }
.nav-item:hover { box-shadow: none !important; transform: none !important; }
.nav-item.active:hover { box-shadow: 0 8px 18px -8px rgba(37,99,235,.5) !important; transform: none !important; }

.nav-group { display: grid; gap: 2px; }
.nav-sub { display: none; gap: 2px; padding: 3px 0 4px 30px; }
.nav-group.open .nav-sub { display: grid; }
.nav-subitem {
  width: 100%; border: 0; background: transparent;
  color: var(--side-text-dim); font-size: 13px; font-weight: 600;
  font-family: var(--font);
  padding: 7px 12px; border-radius: 8px; cursor: pointer; text-align: left;
  white-space: nowrap; overflow: hidden; text-overflow: ellipsis;
  transition: background .14s, color .14s;
  box-shadow: none; transform: none; min-height: 0;
}
.nav-subitem:hover { background: rgba(255,255,255,.05) !important; color: #fff !important; box-shadow: none !important; transform: none !important; border-color: transparent !important; }
.nav-subitem.active { color: #fff !important; background: rgba(37,99,235,.22) !important; border-color: transparent !important; }

.side-foot {
  padding: 12px;
  border-top: 1px solid rgba(255,255,255,.06);
  flex: none;
}
.side-user {
  display: flex; align-items: center; gap: 10px;
  padding: 8px; border-radius: 11px; transition: background .14s;
}
.side-user:hover { background: rgba(255,255,255,.05); }
.side-avatar {
  width: 34px; height: 34px; border-radius: 9px;
  background: rgba(37,99,235,.8);
  display: grid; place-items: center;
  color: #fff; font-weight: 700; font-size: 13px; flex: none;
}
.side-user-name { color: #fff; font-size: 13px; font-weight: 700; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.side-user-role { color: var(--side-text-dim); font-size: 11.5px; font-weight: 600; }

/* logout button in sidebar */
.side-foot .row-act {
  width: 32px; height: 32px; border-radius: 8px; border: 0;
  background: transparent; color: var(--side-text-dim); cursor: pointer;
  display: grid; place-items: center;
  transition: background .12s, color .12s;
  min-height: 0; padding: 0; flex: none;
  box-shadow: none;
}
.side-foot .row-act:hover { background: rgba(255,255,255,.08); color: #fff; box-shadow: none !important; transform: none !important; }

/* ----- Main + Topbar ----- */
.main {
  display: flex;
  flex-direction: column;
  min-width: 0;
  height: 100vh;
  overflow: hidden;
}

.topbar {
  display: flex;
  align-items: center;
  gap: 12px;
  padding: 0 26px;
  height: 66px;
  min-height: 66px;
  flex: none;
  background: rgba(255,255,255,.92);
  border-bottom: 1px solid var(--border);
  backdrop-filter: blur(10px);
  position: sticky; top: 0; z-index: 30;
  /* reset old topbar */
  justify-content: flex-start;
}
.topbar h1 { font-size: 19px; white-space: nowrap; }
.topbar .crumb { font-size: 12.5px; color: var(--faint); font-weight: 600; margin-bottom: 1px; }
.topbar-spacer { flex: 1; }
.topbar-auth-btns { display: flex; gap: 8px; align-items: center; }

.sc-search {
  display: flex; align-items: center; gap: 9px;
  background: var(--surface-2); border: 1px solid var(--border);
  border-radius: 10px; padding: 8px 13px; width: 240px; color: var(--muted);
  transition: border-color .14s, box-shadow .14s;
}
.sc-search:focus-within { border-color: var(--brand); box-shadow: 0 0 0 3px var(--brand-50); background: #fff; }
.sc-search input { border: 0; background: transparent; outline: 0; font-family: var(--font); font-size: 14px; color: var(--ink); width: 100%; min-height: 0; padding: 0; }
.sc-search input::placeholder { color: var(--faint); }

.icon-btn {
  width: 38px; height: 38px; border-radius: 10px;
  border: 1px solid var(--border);
  background: var(--surface); color: var(--muted); cursor: pointer;
  display: grid; place-items: center; position: relative;
  transition: background .14s, color .14s, border-color .14s;
  flex: none; min-height: 0; padding: 0; font-family: var(--font);
  box-shadow: none;
}
.icon-btn:hover { background: var(--surface-2); color: var(--ink); border-color: var(--border-strong); box-shadow: none !important; transform: none !important; }
.icon-btn .dot { position: absolute; top: 9px; right: 9px; width: 7px; height: 7px; border-radius: 50%; background: var(--neg); border: 2px solid var(--surface); }

/* topbar login/register buttons styled as ghost */
.topbar-auth-btns button {
  background: var(--surface);
  border: 1px solid var(--border);
  color: var(--text);
  border-radius: 9px;
  padding: 7px 14px;
  font-family: var(--font);
  font-size: 13px;
  font-weight: 600;
  cursor: pointer;
  transition: background .14s, border-color .14s;
  min-height: 0;
  box-shadow: none;
}
.topbar-auth-btns button:hover { background: var(--surface-2); border-color: var(--border-strong); transform: none !important; box-shadow: none !important; }
.topbar-auth-btns button.primary-btn { background: var(--brand); border-color: var(--brand); color: #fff; }
.topbar-auth-btns button.primary-btn:hover { background: var(--brand-700); border-color: var(--brand-700); }

.content {
  flex: 1;
  overflow-y: auto;
  padding: 26px;
  background: var(--bg);
}
.content-inner { max-width: 1240px; margin: 0 auto; }

/* ----- New Primitives ----- */
.btn {
  display: inline-flex; align-items: center; gap: 8px; justify-content: center;
  border: 1px solid var(--border); background: var(--surface); color: var(--ink);
  padding: 9px 15px; border-radius: 10px; font-size: 14px; font-weight: 600;
  cursor: pointer; font-family: var(--font);
  transition: background .14s, border-color .14s, box-shadow .14s, transform .06s;
  white-space: nowrap; min-height: 0;
}
.btn:hover { background: var(--surface-2); border-color: var(--border-strong); box-shadow: none; transform: none; }
.btn:active { transform: translateY(1px); }
.btn .ico { width: 17px; height: 17px; }
.btn-primary { background: var(--brand); border-color: var(--brand); color: #fff; box-shadow: 0 8px 18px -8px rgba(37,99,235,.4); }
.btn-primary:hover { background: var(--brand-700) !important; border-color: var(--brand-700) !important; box-shadow: 0 8px 18px -8px rgba(37,99,235,.5) !important; transform: none !important; }
.btn-ghost { background: transparent; border-color: transparent; color: var(--muted); }
.btn-ghost:hover { background: var(--surface-2); color: var(--ink); }
.btn-sm { padding: 6px 11px; font-size: 13px; border-radius: 8px; }
.btn-danger { color: var(--neg); border-color: #f9c3c3; background: var(--neg-bg); }
.btn-danger:hover { background: #fad9d9 !important; }

.badge { display: inline-flex; align-items: center; gap: 5px; font-size: 12px; font-weight: 700; padding: 3px 9px; border-radius: 7px; line-height: 1.4; }
.badge::before { content: ""; width: 6px; height: 6px; border-radius: 50%; background: currentColor; opacity: .9; }
.badge-pos   { color: var(--pos);  background: var(--pos-bg); }
.badge-neg   { color: var(--neg);  background: var(--neg-bg); }
.badge-warn  { color: var(--warn); background: var(--warn-bg); }
.badge-info  { color: var(--info); background: var(--info-bg); }
.badge-muted { color: var(--muted); background: var(--surface-2); }
.badge-brand { color: var(--brand); background: var(--brand-50); }

.avatar { width: 36px; height: 36px; border-radius: 10px; display: grid; place-items: center; color: #fff; font-weight: 700; font-size: 13px; flex: none; }
.avatar-sm { width: 30px; height: 30px; border-radius: 8px; font-size: 12px; }

/* .tbl table */
.tbl-wrap { overflow-x: auto; }
table.tbl { width: 100%; border-collapse: collapse; font-size: 14px; }
table.tbl th {
  text-align: left; font-size: 11.5px; font-weight: 700; letter-spacing: .04em; text-transform: uppercase;
  color: var(--faint); padding: 11px 16px; border-bottom: 1px solid var(--border); white-space: nowrap;
  background: var(--surface-2);
}
table.tbl td { padding: var(--pad-y) 16px; border-bottom: 1px solid var(--border); color: var(--text); vertical-align: middle; }
table.tbl tbody tr { transition: background .12s; }
table.tbl tbody tr:hover { background: var(--brand-50); }
table.tbl tbody tr:last-child td { border-bottom: 0; }
.td-strong { color: var(--ink); font-weight: 600; }
.td-right { text-align: right; }
.row-actions { display: flex; gap: 4px; justify-content: flex-end; }
.row-act { width: 32px; height: 32px; border-radius: 8px; border: 1px solid transparent; background: transparent; color: var(--muted); cursor: pointer; display: grid; place-items: center; transition: background .12s, color .12s, border-color .12s; min-height: 0; padding: 0; box-shadow: none; }
.row-act:hover { background: var(--surface-2); color: var(--brand); border-color: var(--border); box-shadow: none !important; transform: none !important; }
.row-act.danger { background: var(--neg); color: #fff; border-color: var(--neg); }
.row-act.danger:hover { background: #b91c1c; border-color: #b91c1c; color: #fff; box-shadow: none !important; transform: none !important; }
button.danger { background: var(--neg); color: #fff; border-color: var(--neg); }
button.danger:hover { background: #b91c1c; border-color: #b91c1c; color: #fff; }

/* fields */
.field { display: grid; gap: 6px; }
.field label { font-size: 13px; font-weight: 600; color: var(--text); margin: 0; }
.field .hint { font-size: 12px; color: var(--faint); }
.input, .select, textarea.input {
  width: 100%; border: 1px solid var(--border-strong); background: var(--surface);
  border-radius: 10px; padding: 10px 13px; font-family: var(--font); font-size: 14px; color: var(--ink);
  outline: 0; transition: border-color .14s, box-shadow .14s;
}
.input:focus, .select:focus, textarea.input:focus { border-color: var(--brand); box-shadow: 0 0 0 3px var(--brand-50); }
.input::placeholder { color: var(--faint); }

/* page layout utilities */
.page-grid { display: grid; gap: var(--gap); }
.stack { display: grid; }
.flex { display: flex; } .items-center { align-items: center; } .between { justify-content: space-between; } .wrap { flex-wrap: wrap; }
.mlauto { margin-left: auto; }
.muted { color: var(--muted); } .faint { color: var(--faint); }
.pos { color: var(--pos); } .neg { color: var(--neg); } .warn { color: var(--warn); }
.fw6 { font-weight: 600; } .fw7 { font-weight: 700; }
.gap-4 { gap: 4px; } .gap-8 { gap: 8px; } .gap-12 { gap: 12px; } .gap-16 { gap: 16px; } .gap-20 { gap: 20px; }

/* density variants */
body.density-compact { --pad-y: 8px; --gap: 14px; --base: 14px; }
body.density-comfy   { --pad-y: 16px; --gap: 26px; --base: 15px; }

/* light sidebar variant */
body.side-light {
  --side-bg: #ffffff; --side-bg-2: #ffffff;
  --side-text: #475569; --side-text-dim: #94a3b8;
}
body.side-light .sidebar { border-right: 1px solid var(--border); }
body.side-light .side-brand-name { color: var(--ink); }
body.side-light .side-logo { background: var(--brand); }
body.side-light .nav-item:hover { background: var(--surface-2); color: var(--ink); }
body.side-light .nav-item.active { color: #fff; }
body.side-light .side-user:hover { background: var(--surface-2); }
body.side-light .side-user-name { color: var(--ink); }
body.side-light .nav-subitem:hover { background: var(--surface-2); color: var(--ink); }

/* page entrance animation */
@keyframes fadeUp { from { transform: translateY(9px); opacity: 0; } to { transform: none; opacity: 1; } }
@media (prefers-reduced-motion: no-preference) {
  .animate { animation: fadeUp .28s ease both; }
}

/* =============================================================
   Auth / Booting states — updated for .shell
   ============================================================= */
body.auth-screen-mode {
  overflow: hidden;
  background: #f4f6f9 !important;
}
body.auth-screen-mode .shell {
  display: block !important;
}
body.auth-screen-mode .sidebar {
  display: none !important;
}
body.auth-screen-mode .topbar {
  display: none !important;
}
body.auth-screen-mode .main {
  min-height: 100vh !important;
  height: auto !important;
  overflow: visible !important;
}
body.auth-screen-mode .content {
  padding: 0 !important;
  overflow: visible !important;
  height: auto !important;
  background: transparent !important;
}

body.app-booting .shell { visibility: hidden; }

body.app-booting::before {
  content: "";
  position: fixed;
  inset: 0; z-index: 999999;
  background:
    radial-gradient(circle at 50% 18%, rgba(91,154,255,.18), transparent 16%),
    linear-gradient(180deg, #0b356f 0%, #082b5a 48%, #071f44 100%);
}
body.app-booting::after {
  content: "SC";
  position: fixed;
  left: 50%; top: 42%;
  z-index: 1000000;
  transform: translate(-50%,-50%);
  width: 58px; height: 58px;
  border-radius: 18px;
  display: grid; place-items: center;
  background: linear-gradient(135deg, #4ea0ff, #2f7df6);
  color: #fff; font-size: 22px; font-weight: 900;
  box-shadow: 0 18px 40px rgba(47,125,246,.36);
}

/* =============================================================
   Mobile responsive
   Match breakpoint to styles.css (1180px) to prevent conflicts
   ============================================================= */
@media (max-width: 1180px) {
  /* Collapse shell to single column */
  .shell {
    grid-template-columns: minmax(0, 1fr) !important;
  }

  /* Sidebar becomes fixed off-canvas */
  .sidebar {
    position: fixed;
    inset: 0 auto 0 0;
    width: min(86vw, 280px);
    z-index: 5000;
    transform: translateX(-104%);
    transition: transform .18s ease;
    box-shadow: 22px 0 55px rgba(13,27,48,.26);
    border-radius: 0;
    display: flex !important;
  }

  body.mobile-sidebar-open .sidebar {
    transform: translateX(0);
  }

  .mobile-sidebar-backdrop {
    position: fixed;
    inset: 0;
    z-index: 4990;
    background: rgba(15,23,42,.48);
    backdrop-filter: blur(2px);
    display: none;
  }

  body.mobile-sidebar-open .mobile-sidebar-backdrop {
    display: block;
  }

  /* Keep mobile-menu-btn visible on mobile */
  .mobile-menu-btn {
    display: inline-grid !important;
    place-items: center;
    width: 38px; min-width: 38px; height: 38px; min-height: 38px;
    padding: 0;
    border-radius: 9px;
    background: var(--brand);
    border-color: var(--brand);
    color: #fff;
    font-size: 18px;
    font-weight: 700;
    flex: none;
    box-shadow: none;
  }
  .mobile-menu-btn:hover { background: var(--brand-700) !important; transform: none !important; box-shadow: none !important; }

  /* Override legacy grid-topbar from styles.css */
  body:not(.auth-screen-mode) .topbar,
  .topbar {
    display: flex !important;
    grid-template-columns: unset !important;
    min-height: 66px;
    padding: 0 14px;
    gap: 10px;
    flex-wrap: nowrap;
    align-items: center;
  }
  body:not(.auth-screen-mode) .topbar > div:first-of-type { min-width: 0; }

  .sc-search { display: none; }
  .content { padding: 14px; }
}
