:root {
  --bg: #f0f4f8;
  --card: #fff;
  --primary: #2563eb;
  --primary-dark: #1d4ed8;
  --primary-soft: #dbeafe;
  --border: #e2e8f0;
  --text: #0f172a;
  --muted: #64748b;
  --ok: #16a34a;
  --warn: #d97706;
  --bad: #dc2626;
  --sidebar: #0f172a;
  --sidebar-text: #cbd5e1;
  --radius: 14px;
  --radius-sm: 10px;
  --shadow: 0 4px 24px rgba(15, 23, 42, .07);
  --shadow-lg: 0 12px 40px rgba(15, 23, 42, .12);
  --topbar-h: 56px;
  --safe-bottom: env(safe-area-inset-bottom, 0px);
  --safe-top: env(safe-area-inset-top, 0px);
  --layout-mobile-ref: 480px;
  --layout-module-max: 100%;
}

@media (min-width: 769px) {
  :root {
    --layout-module-max: min(960px, calc(var(--layout-mobile-ref) * 2));
  }
}

*, *::before, *::after { box-sizing: border-box; }

html {
  -webkit-text-size-adjust: 100%;
  scroll-behavior: smooth;
}

body {
  margin: 0;
  font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, "Helvetica Neue", Arial, sans-serif;
  background: var(--bg);
  color: var(--text);
  line-height: 1.55;
  -webkit-tap-highlight-color: transparent;
  overflow-x: hidden;
}

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

img, video { max-width: 100%; height: auto; }

/* ---- Buttons ---- */
.btn {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  gap: 6px;
  padding: 11px 18px;
  min-height: 44px;
  border-radius: var(--radius-sm);
  border: 1px solid var(--primary);
  background: var(--primary);
  color: #fff;
  font-size: .92rem;
  font-weight: 600;
  cursor: pointer;
  text-decoration: none;
  transition: filter .15s, transform .1s;
  white-space: nowrap;
  touch-action: manipulation;
}
.btn:active { transform: scale(.98); }
.btn:hover { filter: brightness(1.06); text-decoration: none; }
.btn.outline { background: var(--card); color: var(--primary); }
.btn.sm { padding: 7px 12px; min-height: 36px; font-size: .82rem; }
.btn.bad { background: var(--bad); border-color: var(--bad); }
.btn.warn { background: var(--warn); border-color: var(--warn); }
.btn.icon { min-width: 40px; padding: 6px 10px; }

/* ---- Landing ---- */
.landing {
  min-height: 100vh;
  min-height: 100dvh;
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  padding: 24px 16px calc(24px + var(--safe-bottom));
  background: linear-gradient(165deg, #eff6ff 0%, #f8fafc 45%, #eef2ff 100%);
}
.landing h1 {
  font-size: clamp(1.5rem, 5vw, 2rem);
  margin: 0 0 8px;
  color: var(--primary-dark);
  text-align: center;
}
.landing p {
  color: var(--muted);
  margin: 0 0 28px;
  text-align: center;
  max-width: 520px;
  font-size: .95rem;
}
.portal-grid {
  display: grid;
  grid-template-columns: repeat(auto-fit, minmax(260px, 1fr));
  gap: 14px;
  width: 100%;
  max-width: 900px;
}
.portal-card {
  background: var(--card);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  padding: 20px;
  box-shadow: var(--shadow);
  transition: transform .15s, box-shadow .15s;
}
.portal-card:hover {
  transform: translateY(-2px);
  box-shadow: var(--shadow-lg);
}
.portal-card h2 { margin: 0 0 8px; font-size: 1.1rem; }
.portal-card p { margin: 0 0 14px; font-size: .88rem; color: var(--muted); line-height: 1.5; }
.portal-card .btn { width: 100%; }

/* ---- Login ---- */
.login-page {
  min-height: 100vh;
  min-height: 100dvh;
  display: flex;
  align-items: center;
  justify-content: center;
  padding: 20px 16px calc(20px + var(--safe-bottom));
  background: linear-gradient(165deg, #eff6ff 0%, #f8fafc 50%, #eef2ff 100%);
}
.login-box {
  width: 100%;
  max-width: 400px;
  padding: 28px 24px;
  background: var(--card);
  border-radius: var(--radius);
  box-shadow: var(--shadow-lg);
  border: 1px solid var(--border);
}
.login-box h1 { margin: 0 0 6px; color: var(--primary-dark); font-size: 1.45rem; }
.login-box .login-sub { color: var(--muted); margin: 0 0 20px; font-size: .9rem; }
.login-box label { display: block; margin: 14px 0 5px; font-size: .85rem; color: var(--muted); font-weight: 500; }
.login-box input {
  width: 100%;
  padding: 12px 14px;
  border-radius: var(--radius-sm);
  border: 1px solid var(--border);
  font-size: 16px;
  min-height: 48px;
}
.login-box input:focus { outline: 2px solid var(--primary-soft); border-color: var(--primary); }
.login-box .btn { width: 100%; margin-top: 20px; }
.login-box .err { color: var(--bad); font-size: .88rem; margin-top: 10px; }

/* ---- App shell ---- */
.admin-shell {
  display: block;
  min-height: 100vh;
  min-height: 100dvh;
}

.sidebar-backdrop {
  display: none;
  position: fixed;
  inset: 0;
  background: rgba(15, 23, 42, .5);
  z-index: 200;
  opacity: 0;
  transition: opacity .25s;
  pointer-events: none;
}
body.sidebar-open .sidebar-backdrop {
  display: block;
  opacity: 1;
  pointer-events: auto;
}

.sidebar {
  position: fixed;
  top: 0;
  left: 0;
  bottom: 0;
  width: min(280px, 88vw);
  background: linear-gradient(180deg, #0f172a 0%, #1e293b 100%);
  color: var(--sidebar-text);
  padding: 0;
  display: flex;
  flex-direction: column;
  min-height: 100vh;
  min-height: 100dvh;
  z-index: 210;
  transform: translateX(-100%);
  transition: transform .28s cubic-bezier(.4, 0, .2, 1);
  box-shadow: var(--shadow-lg);
}
body.sidebar-open .sidebar { transform: translateX(0); }
.sidebar .brand {
  padding: 20px 18px 16px;
  font-weight: 700;
  font-size: 1.05rem;
  color: #fff;
  border-bottom: 1px solid rgba(255,255,255,.08);
  letter-spacing: -.01em;
}
.sidebar nav { padding: 10px 0; flex: 1; overflow-y: auto; }
.sidebar nav a {
  display: flex;
  align-items: center;
  padding: 12px 18px;
  color: var(--sidebar-text);
  text-decoration: none;
  font-size: .9rem;
  font-weight: 500;
  border-left: 3px solid transparent;
  transition: background .15s, color .15s;
}
.sidebar nav a:hover,
.sidebar nav a.active {
  background: rgba(255,255,255,.06);
  color: #fff;
  border-left-color: #60a5fa;
  text-decoration: none;
}
.sidebar-close {
  display: flex;
  position: absolute;
  top: 14px;
  right: 14px;
  width: 36px;
  height: 36px;
  border: none;
  background: rgba(255,255,255,.1);
  color: #fff;
  border-radius: 8px;
  font-size: 1.1rem;
  cursor: pointer;
  align-items: center;
  justify-content: center;
}
.sidebar .brand { padding-right: 48px; }

.sidebar-user {
  margin-top: auto;
  padding: 16px 18px calc(16px + var(--safe-bottom));
  border-top: 1px solid rgba(255,255,255,.08);
  font-size: .88rem;
}
.sidebar-user .gv-name { font-weight: 700; color: #fff; margin-bottom: 8px; }
.sidebar-user .gv-actions { display: flex; align-items: center; gap: 8px; flex-wrap: wrap; }
.sidebar-user .gv-actions-col { flex-direction: column; align-items: flex-start; gap: 6px; }
.sidebar-user .gv-actions a { color: #93c5fd; font-size: .82rem; text-decoration: none; padding: 4px 0; min-height: 32px; display: inline-flex; align-items: center; }

.main-wrapper {
  flex: 1;
  display: flex;
  flex-direction: column;
  min-width: 0;
  min-height: 100vh;
  min-height: 100dvh;
}

.app-topbar {
  display: flex;
  align-items: center;
  gap: 12px;
  height: var(--topbar-h);
  padding: 0 12px 0 8px;
  padding-top: var(--safe-top);
  background: var(--card);
  border-bottom: 1px solid var(--border);
  position: sticky;
  top: 0;
  z-index: 100;
  box-shadow: 0 1px 8px rgba(15,23,42,.05);
  width: 100%;
}
.topbar-menu {
  display: flex;
  align-items: center;
  justify-content: center;
  width: 44px;
  height: 44px;
  border: none;
  background: transparent;
  color: var(--text);
  border-radius: var(--radius-sm);
  cursor: pointer;
  flex-shrink: 0;
}
.topbar-menu:active { background: var(--primary-soft); }
.topbar-title {
  font-size: 1rem;
  font-weight: 700;
  color: var(--text);
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
  flex: 1;
}

.main {
  flex: 1;
  display: flex;
  flex-direction: column;
  align-items: center;
  padding: 16px 12px calc(16px + var(--safe-bottom));
  overflow-x: hidden;
  background: var(--bg);
  width: 100%;
}

.main > .section {
  width: 100%;
  max-width: var(--layout-module-max);
  margin-left: auto;
  margin-right: auto;
}

.module-scaffold,
.profile-scaffold {
  width: 100%;
  max-width: 100%;
}

.page-title {
  margin: 0 0 20px;
  font-size: clamp(1.25rem, 4vw, 1.5rem);
  font-weight: 700;
  letter-spacing: -.02em;
}

/* ---- Stats & cards ---- */
.stats {
  display: grid;
  grid-template-columns: repeat(auto-fit, minmax(130px, 1fr));
  gap: 10px;
  margin-bottom: 20px;
}
.stat {
  background: var(--card);
  border-radius: var(--radius);
  padding: 16px;
  border: 1px solid var(--border);
  box-shadow: var(--shadow);
}
.stat b {
  display: block;
  font-size: clamp(1.3rem, 4vw, 1.6rem);
  color: var(--primary);
  font-weight: 700;
  line-height: 1.2;
}
.stat span { font-size: .8rem; color: var(--muted); line-height: 1.35; display: block; margin-top: 4px; }

.card {
  background: var(--card);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  padding: 16px;
  margin-bottom: 14px;
  box-shadow: var(--shadow);
  width: 100%;
  max-width: 100%;
}
.card h3 { margin: 0 0 12px; font-size: 1rem; font-weight: 700; }

.toolbar {
  display: flex;
  flex-wrap: wrap;
  gap: 8px;
  align-items: center;
  margin-bottom: 12px;
}
.toolbar label {
  font-size: .85rem;
  color: var(--muted);
  display: flex;
  align-items: center;
  gap: 6px;
  flex-wrap: wrap;
}

/* ---- Forms ---- */
input, select, textarea {
  padding: 10px 12px;
  border: 1px solid var(--border);
  border-radius: var(--radius-sm);
  font-size: 16px;
  font-family: inherit;
  background: var(--card);
  color: var(--text);
  min-height: 44px;
}
textarea { min-height: 80px; resize: vertical; }
input:focus, select:focus, textarea:focus {
  outline: 2px solid var(--primary-soft);
  border-color: var(--primary);
}

.form-grid {
  display: grid;
  grid-template-columns: repeat(auto-fill, minmax(200px, 1fr));
  gap: 12px;
  margin-bottom: 14px;
}
.form-grid label {
  display: flex;
  flex-direction: column;
  gap: 5px;
  font-size: .8rem;
  color: var(--muted);
  font-weight: 500;
}
.form-grid label input,
.form-grid label select,
.form-grid label textarea { width: 100%; }

/* ---- Tables ---- */
.table-scroll {
  width: 100%;
  max-width: 100%;
  overflow-x: auto;
  -webkit-overflow-scrolling: touch;
  margin: 0;
}

table {
  width: 100%;
  max-width: 100%;
  border-collapse: collapse;
  font-size: clamp(.72rem, 2.8vw, .86rem);
  table-layout: auto;
}
th, td {
  border: 1px solid var(--border);
  padding: clamp(6px, 2vw, 10px);
  text-align: left;
  vertical-align: middle;
  word-break: break-word;
  overflow-wrap: anywhere;
}
th {
  background: #f8fafc;
  font-weight: 600;
  font-size: clamp(.68rem, 2.5vw, .8rem);
  white-space: normal;
}
.card > table,
.modal table,
.modal-box table {
  min-width: 0;
}

.badge {
  display: inline-block;
  padding: 3px 9px;
  border-radius: 999px;
  font-size: .72rem;
  font-weight: 600;
  white-space: nowrap;
}
.badge.ok { background: #dcfce7; color: var(--ok); }
.badge.warn { background: #fef3c7; color: var(--warn); }
.badge.bad { background: #fee2e2; color: var(--bad); }
.badge.gray { background: #f1f5f9; color: var(--muted); }

.section { display: none; }
.section.active { display: block; }
.hidden { display: none !important; }
.empty { color: var(--muted); font-style: italic; padding: 16px; text-align: center; }

/* ---- Modals ---- */
.modal-bg {
  position: fixed;
  inset: 0;
  background: rgba(15, 23, 42, .55);
  z-index: 300;
  display: flex;
  align-items: flex-end;
  justify-content: center;
  padding: 0;
}
.modal-bg.hidden { display: none !important; }

.modal, .modal-box {
  background: var(--card);
  border-radius: var(--radius) var(--radius) 0 0;
  max-width: var(--layout-module-max);
  width: 100%;
  max-height: 92vh;
  max-height: 92dvh;
  overflow: auto;
  padding: 20px;
  padding-bottom: calc(20px + var(--safe-bottom));
  box-shadow: var(--shadow-lg);
  -webkit-overflow-scrolling: touch;
}
.modal-hd {
  display: flex;
  justify-content: space-between;
  align-items: center;
  margin-bottom: 14px;
  position: sticky;
  top: 0;
  background: var(--card);
  z-index: 1;
  padding-bottom: 8px;
}
.modal-hd h2, .modal-box h3 { margin: 0; font-size: 1.1rem; }
.close-btn {
  border: none;
  background: #f1f5f9;
  width: 40px;
  height: 40px;
  border-radius: var(--radius-sm);
  cursor: pointer;
  font-size: 1.2rem;
  flex-shrink: 0;
  display: flex;
  align-items: center;
  justify-content: center;
}
.modal-box label { display: block; margin-bottom: 12px; font-size: .88rem; color: var(--muted); }
.modal-box input { width: 100%; margin-top: 4px; }

/* ---- TKB (2x4 grid, sticky-note style) ---- */
.tkb-week {
  --tkb-gap: 10px;
  display: grid;
  grid-template-columns: repeat(2, minmax(0, 1fr));
  gap: var(--tkb-gap);
  width: 100%;
  max-width: var(--layout-module-max);
  margin: 0 auto;
  font-size: clamp(0.78rem, 2.8vw, 0.92rem);
}
.tkb-day {
  position: relative;
  display: flex;
  flex-direction: column;
  min-width: 0;
  min-height: 148px;
  padding: 0 10px 10px;
  border: 2px dashed #d4b84a;
  border-radius: 14px;
  background: linear-gradient(180deg, #fffef8 0%, #fff6d6 55%, #fff0c4 100%);
  box-shadow: 0 2px 8px rgba(120, 90, 20, .08);
}
.tkb-day::before,
.tkb-day::after {
  content: '';
  position: absolute;
  top: 7px;
  width: 9px;
  height: 9px;
  border-radius: 50%;
  background: #f0c929;
  box-shadow: inset 0 1px 2px rgba(255,255,255,.5);
}
.tkb-day::before { left: 12px; }
.tkb-day::after { right: 12px; }
.tkb-day-empty {
  visibility: hidden;
  pointer-events: none;
  border: none;
  background: transparent;
  box-shadow: none;
  min-height: 0;
}
.tkb-day-empty::before,
.tkb-day-empty::after { display: none; }
.tkb-day-sun { grid-column: 1 / -1; }
.tkb-day-hd {
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 0;
  margin: 12px 6px 8px;
  padding: 7px 14px;
  border-radius: 999px;
  background: linear-gradient(90deg, #f7e08a 0%, #ebc93f 50%, #f7e08a 100%);
  color: #3d2f0a;
  text-align: center;
  box-shadow: inset 0 1px 0 rgba(255,255,255,.45);
}
.tkb-day-name {
  font-weight: 700;
  font-size: 1em;
  line-height: 1.3;
  white-space: nowrap;
}
.tkb-day-date { display: none; }
.tkb-day-body {
  flex: 1;
  display: flex;
  flex-direction: column;
  gap: 0;
  min-height: 0;
}
.tkb-slot {
  position: relative;
  padding: 7px 6px 7px 26px;
  border-bottom: 1px solid rgba(180, 150, 60, .35);
  cursor: pointer;
  line-height: 1.35;
  background: transparent;
}
.tkb-slot:last-of-type { border-bottom: none; }
.tkb-slot::before {
  content: '';
  position: absolute;
  left: 2px;
  top: 9px;
  width: 14px;
  height: 14px;
  border: 2px solid #d4b84a;
  border-radius: 3px;
  background: #fffef9;
}
.tkb-slot:hover { background: rgba(255,255,255,.45); }
.tkb-slot-line {
  display: block;
  white-space: normal;
  overflow: visible;
  word-break: break-word;
  line-height: 1.35;
}
.tkb-slot-line1 { font-weight: 700; color: #3d2f0a; font-size: .92em; }
.tkb-slot-line2 { color: #4a3f20; font-size: .88em; }
.tkb-slot-line3 { color: #6b5a28; font-size: .82em; }
.tkb-add {
  margin-top: auto;
  border: 2px dashed #d4c48a;
  border-radius: 8px;
  padding: 8px;
  text-align: center;
  color: #8a7340;
  cursor: pointer;
  font-size: .82em;
  min-height: 36px;
  display: flex;
  align-items: center;
  justify-content: center;
  background: rgba(255,255,255,.35);
}
.tkb-add:hover { border-color: #c9a227; color: #5c4a12; background: rgba(255,255,255,.6); }
.tkb-toolbar { padding: 0; margin-bottom: 12px; }
.tkb-week-bar {
  display: flex;
  align-items: center;
  justify-content: flex-start;
  gap: 6px;
  flex-wrap: nowrap;
  width: 100%;
}
.tkb-week-bar-title {
  font-size: .9rem;
  font-weight: 600;
  color: var(--text);
  white-space: nowrap;
  flex-shrink: 0;
}
.tkb-week-bar .tkb-week-nav {
  flex: 0 0 auto;
  width: 36px;
  min-width: 36px;
  max-width: 36px;
  padding: 0;
  justify-content: center;
}
.tkb-week-field {
  position: relative;
  display: inline-flex;
  align-items: center;
  justify-content: center;
  gap: 8px;
  min-width: 9.5rem;
  padding: 7px 10px 7px 12px;
  border: 1px solid var(--border);
  border-radius: var(--radius-sm);
  background: var(--card);
  cursor: pointer;
  font-weight: 600;
  font-size: .88rem;
  color: var(--text);
  touch-action: manipulation;
}
.tkb-week-field:hover { border-color: var(--primary); background: var(--primary-soft); }
.tkb-week-display-text {
  white-space: nowrap;
  line-height: 1.2;
  pointer-events: none;
  user-select: none;
}
.tkb-cal-icon {
  flex-shrink: 0;
  color: var(--primary);
  opacity: .9;
  pointer-events: none;
}
.tkb-week-input-overlay {
  position: absolute;
  inset: 0;
  width: 100%;
  height: 100%;
  margin: 0;
  padding: 0;
  opacity: 0;
  cursor: pointer;
  border: none;
  background: transparent;
  font-size: 16px;
}
.tkb-week-input-overlay::-webkit-calendar-picker-indicator {
  position: absolute;
  inset: 0;
  width: 100%;
  height: 100%;
  margin: 0;
  padding: 0;
  cursor: pointer;
}
.tkb-toolbar .week-nav { display: inline-flex; align-items: center; gap: 4px; }
.tkb-toolbar .week-range { display: none; }

/* ---- Mobile tweaks ---- */
@media (max-width: 768px) {
  .main .page-title { display: none; }

  .stats { grid-template-columns: repeat(2, 1fr); gap: 8px; }
  .stat { padding: 12px; }
  .stat b { font-size: 1.25rem; }

  .card { padding: 14px; border-radius: var(--radius-sm); }

  .form-grid { grid-template-columns: 1fr; }

  .toolbar { flex-direction: column; align-items: stretch; }
  .toolbar label { width: 100%; }
  .toolbar input, .toolbar select { flex: 1; width: 100%; }
  .toolbar .btn { width: 100%; }
  .tkb-toolbar.toolbar { flex-direction: row; align-items: center; }
  .tkb-toolbar .btn { width: auto; }
  .tkb-week-bar { flex: 1; min-width: 0; }
  .tkb-week-pick { flex: 1 1 auto; min-width: 0; max-width: 14rem; }
  .tkb-week-field { flex: 1 1 auto; min-width: 0; max-width: 14rem; }

  .portal-grid { grid-template-columns: 1fr; }

  .modal-bg { align-items: flex-end; }
  .modal, .modal-box {
    max-height: 94vh;
    max-height: 94dvh;
    border-radius: 16px 16px 0 0;
    width: 100%;
    max-width: 100%;
  }
}

@media (min-width: 769px) {
  .main { padding: 20px 16px 28px; }

  .modal-bg {
    align-items: center;
    padding: 16px;
  }
  .modal, .modal-box {
    border-radius: var(--radius);
    max-height: 90vh;
    width: 100%;
    max-width: var(--layout-module-max);
  }
}

@media (min-width: 1024px) {
  .main { padding: 24px 20px 32px; }
}
