:root {
  --bg: #0b1120;
  --bg-elev: #111827;
  --card: #162033;
  --card-hover: #1c2942;
  --border: rgba(148, 163, 184, 0.18);
  --text: #f1f5f9;
  --muted: #94a3b8;
  --primary: #0ea5e9;
  --primary-strong: #0284c7;
  --accent: #10b981;
  --warning: #f59e0b;
  --danger: #ef4444;
  --shadow: 0 18px 45px rgba(2, 6, 23, 0.55);
  --radius: 14px;
  --ui-zoom: 1;
}

html[data-theme="light"] {
  --bg: #f1f5f9;
  --bg-elev: #ffffff;
  --card: #ffffff;
  --card-hover: #f8fafc;
  --border: rgba(15, 23, 42, 0.12);
  --text: #0f172a;
  --muted: #475569;
  --primary: #0284c7;
  --primary-strong: #0369a1;
  --accent: #059669;
  --warning: #d97706;
  --danger: #dc2626;
  --shadow: 0 16px 38px rgba(15, 23, 42, 0.14);
}

* { box-sizing: border-box; }

html {
  font-size: calc(16px * var(--ui-zoom));
  -webkit-text-size-adjust: 100%;
}

body {
  margin: 0;
  font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, Inter, sans-serif;
  background: var(--bg);
  color: var(--text);
  min-height: 100dvh;
  transition: background 0.2s ease, color 0.2s ease;
}

.hidden { display: none !important; }
.muted { color: var(--muted); font-size: 0.9rem; margin: 0.2rem 0 0; }

/* BUTTONS & INPUTS */
.btn {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  gap: 0.4rem;
  border: 1px solid transparent;
  border-radius: 10px;
  padding: 0.55rem 0.95rem;
  font-size: 0.9rem;
  font-weight: 600;
  cursor: pointer;
  transition: transform 0.12s ease, background 0.15s ease, border-color 0.15s ease;
  color: var(--text);
  background: var(--card);
}
.btn:active { transform: scale(0.97); }
.btn-primary { background: linear-gradient(135deg, var(--primary), #2563eb); color: #fff; }
.btn-primary:hover { filter: brightness(1.08); }
.btn-secondary { background: rgba(14, 165, 233, 0.14); border-color: rgba(14, 165, 233, 0.35); color: var(--primary); }
.btn-secondary:hover { background: rgba(14, 165, 233, 0.24); }
.btn-success { background: linear-gradient(135deg, #10b981, #059669); color: #fff; }
.btn-danger { background: rgba(239, 68, 68, 0.14); border-color: rgba(239, 68, 68, 0.4); color: var(--danger); }
.btn-ghost { background: transparent; border-color: var(--border); }
.btn-ghost:hover { background: var(--card-hover); }
.btn-sm { padding: 0.38rem 0.7rem; font-size: 0.82rem; border-radius: 8px; }
.btn-lg { padding: 0.75rem 1.2rem; font-size: 1rem; width: 100%; }

.btn-icon {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  gap: 0.25rem;
  min-width: 36px;
  height: 36px;
  padding: 0 0.5rem;
  border-radius: 10px;
  border: 1px solid var(--border);
  background: var(--card);
  color: var(--text);
  cursor: pointer;
  font-size: 0.95rem;
}
.btn-icon:hover { background: var(--card-hover); border-color: var(--primary); }

input, select, textarea {
  width: 100%;
  padding: 0.6rem 0.78rem;
  border-radius: 10px;
  border: 1px solid var(--border);
  background: var(--bg);
  color: var(--text);
  font-size: 0.95rem;
  font-family: inherit;
}
input:focus, select:focus, textarea:focus {
  outline: none;
  border-color: var(--primary);
  box-shadow: 0 0 0 3px rgba(14, 165, 233, 0.18);
}

/* PASSWORD BOX WITH EYE TOGGLE */
.password-box {
  position: relative;
  display: flex;
  align-items: center;
}
.password-box input { padding-right: 2.8rem; }
.eye-btn {
  position: absolute;
  right: 0.35rem;
  top: 50%;
  transform: translateY(-50%);
  width: 34px;
  height: 32px;
  border: none;
  border-radius: 8px;
  background: transparent;
  cursor: pointer;
  font-size: 1.05rem;
  display: flex;
  align-items: center;
  justify-content: center;
  color: var(--muted);
}
.eye-btn:hover { background: var(--card-hover); color: var(--text); }

.field { display: flex; flex-direction: column; gap: 0.35rem; }
.field > span { font-size: 0.85rem; font-weight: 600; color: var(--muted); }
.form-stack { display: flex; flex-direction: column; gap: 0.85rem; }
.form-row { display: grid; grid-template-columns: repeat(2, minmax(0, 1fr)); gap: 0.75rem; }

.alert-error {
  padding: 0.65rem 0.85rem;
  border-radius: 10px;
  background: rgba(239, 68, 68, 0.14);
  border: 1px solid rgba(239, 68, 68, 0.4);
  color: var(--danger);
  font-size: 0.88rem;
}

/* LOGIN SCREEN */
.login-screen {
  min-height: 100dvh;
  display: flex;
  align-items: center;
  justify-content: center;
  padding: 1.25rem;
  background: radial-gradient(circle at top right, rgba(14, 165, 233, 0.18), transparent 55%),
              radial-gradient(circle at bottom left, rgba(16, 185, 129, 0.14), transparent 55%),
              var(--bg);
}
.login-card {
  width: 100%;
  max-width: 420px;
  background: var(--bg-elev);
  border: 1px solid var(--border);
  border-radius: 18px;
  padding: 1.75rem;
  box-shadow: var(--shadow);
}
.login-brand { display: flex; align-items: center; gap: 0.9rem; margin-bottom: 1.4rem; }
.brand-logo { width: 54px; height: 54px; border-radius: 14px; }
.login-brand h1 { margin: 0; font-size: 1.3rem; }
.login-brand .subtitle { margin: 0.2rem 0 0; font-size: 0.82rem; color: var(--muted); }
.login-footer {
  margin-top: 1.2rem;
  padding-top: 1rem;
  border-top: 1px solid var(--border);
  display: flex;
  justify-content: space-between;
  align-items: center;
}
.zoom-mini { display: flex; align-items: center; gap: 0.35rem; }
.zoom-label { font-size: 0.82rem; font-weight: 700; min-width: 42px; text-align: center; }

/* TOPBAR */
.app-shell { display: flex; flex-direction: column; min-height: 100dvh; }
.topbar {
  position: sticky;
  top: 0;
  z-index: 40;
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 0.75rem;
  padding: calc(0.65rem + env(safe-area-inset-top)) 1rem 0.65rem;
  background: color-mix(in srgb, var(--bg-elev) 88%, transparent);
  backdrop-filter: blur(14px);
  border-bottom: 1px solid var(--border);
}
.topbar-left { display: flex; align-items: center; gap: 0.65rem; min-width: 0; }
.topbar-logo { width: 38px; height: 38px; border-radius: 10px; flex-shrink: 0; }
.topbar-name { font-weight: 800; font-size: 1rem; display: flex; align-items: center; gap: 0.45rem; white-space: nowrap; }
.topbar-sub { font-size: 0.78rem; color: var(--muted); white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.badge-ver {
  font-size: 0.7rem;
  font-weight: 700;
  padding: 0.12rem 0.45rem;
  border-radius: 999px;
  background: rgba(14, 165, 233, 0.16);
  color: var(--primary);
  border: 1px solid rgba(14, 165, 233, 0.32);
}

.desktop-nav { display: flex; gap: 0.35rem; }
.nav-btn {
  padding: 0.48rem 0.85rem;
  border-radius: 10px;
  border: 1px solid transparent;
  background: transparent;
  color: var(--muted);
  font-weight: 600;
  font-size: 0.88rem;
  cursor: pointer;
}
.nav-btn:hover { background: var(--card); color: var(--text); }
.nav-btn.active {
  background: rgba(14, 165, 233, 0.15);
  border-color: rgba(14, 165, 233, 0.35);
  color: var(--primary);
}

.topbar-actions { display: flex; align-items: center; gap: 0.4rem; }
.zoom-control {
  display: inline-flex;
  align-items: center;
  gap: 0.2rem;
  padding: 0.15rem;
  border-radius: 11px;
  background: var(--bg);
  border: 1px solid var(--border);
}
.zoom-control .btn-icon { border: none; background: transparent; height: 30px; min-width: 28px; }
.zoom-pill { font-size: 0.8rem; font-weight: 700; padding: 0 0.4rem !important; }

/* MAIN CONTENT */
.main-content {
  width: 100%;
  max-width: 1220px;
  margin: 0 auto;
  padding: 1.15rem 1rem calc(5.2rem + env(safe-area-inset-bottom));
  flex: 1;
}

/* TODAY HERO */
.today-hero {
  display: flex;
  flex-wrap: wrap;
  align-items: center;
  justify-content: space-between;
  gap: 1rem;
  padding: 1.1rem 1.25rem;
  border-radius: var(--radius);
  background: linear-gradient(135deg, rgba(14, 165, 233, 0.14), rgba(16, 185, 129, 0.10));
  border: 1px solid var(--border);
  margin-bottom: 1.2rem;
}
.hero-left { flex: 1; min-width: 260px; }
.date-stepper { display: flex; align-items: center; gap: 0.45rem; flex-wrap: wrap; margin-bottom: 0.65rem; }
.date-input { width: auto; min-width: 155px; padding: 0.42rem 0.65rem; font-weight: 600; }
.hero-stats { display: flex; justify-content: space-between; align-items: center; font-size: 0.92rem; margin-bottom: 0.4rem; }
.progress-track { height: 10px; border-radius: 999px; background: rgba(148, 163, 184, 0.22); overflow: hidden; }
.progress-fill { height: 100%; background: linear-gradient(90deg, var(--primary), var(--accent)); border-radius: 999px; transition: width 0.35s ease; }
.hero-right { display: flex; gap: 0.55rem; flex-wrap: wrap; }

/* SLOT GROUPS */
.slots-stack { display: flex; flex-direction: column; gap: 1.2rem; }
.slot-card {
  background: var(--bg-elev);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  overflow: hidden;
}
.slot-header {
  display: flex;
  flex-wrap: wrap;
  align-items: center;
  justify-content: space-between;
  gap: 0.65rem;
  padding: 0.85rem 1.1rem;
  background: rgba(14, 165, 233, 0.07);
  border-bottom: 1px solid var(--border);
}
.slot-title-wrap { display: flex; align-items: center; gap: 0.6rem; }
.slot-title { font-size: 1.08rem; font-weight: 800; }
.slot-time-badge {
  font-size: 0.85rem;
  font-weight: 700;
  padding: 0.2rem 0.6rem;
  border-radius: 999px;
  background: var(--card);
  border: 1px solid var(--border);
}
.slot-count { font-size: 0.82rem; color: var(--muted); font-weight: 600; }
.slot-actions { display: flex; align-items: center; gap: 0.45rem; }

.dose-list { display: grid; grid-template-columns: repeat(auto-fill, minmax(320px, 1fr)); gap: 0.85rem; padding: 0.95rem; }

/* DOSE ITEM CARD */
.dose-item {
  display: flex;
  gap: 0.8rem;
  align-items: center;
  padding: 0.78rem;
  border-radius: 12px;
  background: var(--card);
  border: 1px solid var(--border);
  transition: border-color 0.2s, transform 0.15s, background 0.2s;
}
.dose-item.taken {
  border-color: rgba(16, 185, 129, 0.55);
  background: color-mix(in srgb, var(--accent) 10%, var(--card));
}
.dose-item.high-risk { border-left: 4px solid var(--danger); }

.box-thumb-wrap {
  position: relative;
  width: 92px;
  height: 64px;
  flex-shrink: 0;
  border-radius: 9px;
  overflow: hidden;
  background: #ffffff;
  border: 1px solid var(--border);
  cursor: zoom-in;
}
.box-thumb-wrap img {
  width: 100%;
  height: 100%;
  object-fit: contain;
  display: block;
}
.zoom-badge {
  position: absolute;
  right: 4px;
  bottom: 4px;
  width: 24px;
  height: 24px;
  border-radius: 7px;
  border: none;
  background: rgba(15, 23, 42, 0.78);
  color: #fff;
  font-size: 0.75rem;
  display: flex;
  align-items: center;
  justify-content: center;
  cursor: zoom-in;
}

.dose-info { flex: 1; min-width: 0; }
.dose-name { font-weight: 800; font-size: 0.96rem; line-height: 1.25; }
.dose-drug { font-size: 0.8rem; color: var(--muted); white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.dose-meta { display: flex; flex-wrap: wrap; gap: 0.35rem; margin-top: 0.38rem; align-items: center; }

.pill-qty {
  font-size: 0.78rem;
  font-weight: 800;
  padding: 0.16rem 0.55rem;
  border-radius: 999px;
  background: rgba(14, 165, 233, 0.16);
  color: var(--primary);
}
.pill-risk {
  font-size: 0.72rem;
  font-weight: 800;
  padding: 0.14rem 0.5rem;
  border-radius: 999px;
  background: rgba(239, 68, 68, 0.18);
  color: var(--danger);
}
.pill-stock {
  font-size: 0.72rem;
  padding: 0.14rem 0.48rem;
  border-radius: 999px;
  background: rgba(148, 163, 184, 0.15);
  color: var(--muted);
}
.pill-stock.low { background: rgba(245, 158, 11, 0.2); color: var(--warning); font-weight: 700; }
.dose-note { font-size: 0.74rem; color: var(--muted); margin-top: 0.25rem; }

.take-btn {
  flex-shrink: 0;
  min-width: 86px;
  height: 46px;
  border-radius: 12px;
  border: 2px solid var(--border);
  background: var(--bg);
  color: var(--text);
  font-weight: 800;
  font-size: 0.84rem;
  cursor: pointer;
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  line-height: 1.1;
}
.take-btn:hover { border-color: var(--primary); }
.take-btn.taken {
  background: var(--accent);
  border-color: var(--accent);
  color: #fff;
}

/* SECTION HEADER & MEDS GRID */
.section-header {
  display: flex;
  flex-wrap: wrap;
  align-items: center;
  justify-content: space-between;
  gap: 0.85rem;
  margin-bottom: 1.1rem;
}
.section-header h2 { margin: 0; font-size: 1.3rem; }
.section-actions { display: flex; gap: 0.55rem; flex-wrap: wrap; align-items: center; }
.search-input { width: 240px; }
.select-input { width: auto; }

.meds-grid { display: grid; grid-template-columns: repeat(auto-fill, minmax(300px, 1fr)); gap: 1rem; }
.med-card {
  background: var(--bg-elev);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  overflow: hidden;
  display: flex;
  flex-direction: column;
}
.med-card-img {
  position: relative;
  height: 170px;
  background: #ffffff;
  border-bottom: 1px solid var(--border);
  display: flex;
  align-items: center;
  justify-content: center;
  padding: 0.5rem;
  cursor: zoom-in;
}
.med-card-img img { max-width: 100%; max-height: 100%; object-fit: contain; border-radius: 6px; }
.med-card-img .zoom-badge { width: 32px; height: 32px; font-size: 0.95rem; right: 10px; bottom: 10px; }
.med-card-body { padding: 0.95rem; display: flex; flex-direction: column; gap: 0.45rem; flex: 1; }
.med-card-title { font-weight: 800; font-size: 1.04rem; display: flex; justify-content: space-between; gap: 0.4rem; align-items: flex-start; }
.med-cat { font-size: 0.72rem; padding: 0.15rem 0.5rem; border-radius: 999px; background: rgba(14, 165, 233, 0.14); color: var(--primary); font-weight: 700; white-space: nowrap; }
.med-sch-chips { display: flex; flex-wrap: wrap; gap: 0.35rem; margin-top: 0.25rem; }
.sch-chip {
  font-size: 0.76rem;
  padding: 0.2rem 0.5rem;
  border-radius: 8px;
  background: var(--card);
  border: 1px solid var(--border);
  cursor: pointer;
}
.sch-chip:hover { border-color: var(--primary); }
.med-card-footer {
  margin-top: auto;
  padding: 0.7rem 0.95rem;
  border-top: 1px solid var(--border);
  display: flex;
  justify-content: space-between;
  align-items: center;
  gap: 0.4rem;
}

/* HISTORY */
.history-calendar {
  display: grid;
  grid-template-columns: repeat(auto-fill, minmax(112px, 1fr));
  gap: 0.65rem;
  margin-bottom: 1.2rem;
}
.day-tile {
  padding: 0.75rem;
  border-radius: 12px;
  background: var(--bg-elev);
  border: 1px solid var(--border);
  text-align: center;
}
.day-tile.full { border-color: rgba(16, 185, 129, 0.55); background: rgba(16, 185, 129, 0.08); }
.day-tile-date { font-size: 0.78rem; color: var(--muted); font-weight: 600; }
.day-tile-pct { font-size: 1.2rem; font-weight: 800; margin: 0.25rem 0; }
.day-tile-cnt { font-size: 0.76rem; color: var(--muted); }

.card-panel {
  background: var(--bg-elev);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  padding: 1rem 1.15rem;
}
.card-panel h3 { margin: 0 0 0.8rem; font-size: 1.05rem; }
.history-logs { display: flex; flex-direction: column; gap: 0.45rem; }
.log-row {
  display: flex;
  flex-wrap: wrap;
  justify-content: space-between;
  gap: 0.5rem;
  padding: 0.55rem 0.75rem;
  border-radius: 10px;
  background: var(--card);
  border: 1px solid var(--border);
  font-size: 0.88rem;
}

/* USERS GRID */
.users-grid { display: grid; grid-template-columns: repeat(auto-fill, minmax(300px, 1fr)); gap: 0.9rem; }
.user-card {
  background: var(--bg-elev);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  padding: 1rem;
  display: flex;
  flex-direction: column;
  gap: 0.55rem;
}
.user-top { display: flex; justify-content: space-between; align-items: center; }
.role-badge { font-size: 0.75rem; font-weight: 800; padding: 0.18rem 0.55rem; border-radius: 999px; text-transform: uppercase; background: rgba(14, 165, 233, 0.16); color: var(--primary); }
.role-badge.admin { background: rgba(16, 185, 129, 0.18); color: var(--accent); }

/* FLOATING DRAGGABLE & RESIZABLE WINDOWS */
.windows-layer {
  position: fixed;
  inset: 0;
  pointer-events: none;
  z-index: 100;
}
.float-win {
  position: fixed;
  pointer-events: auto;
  display: flex;
  flex-direction: column;
  background: var(--bg-elev);
  border: 1px solid var(--border);
  border-radius: 16px;
  box-shadow: var(--shadow);
  min-width: 300px;
  min-height: 220px;
  max-width: 96vw;
  max-height: 92dvh;
  overflow: hidden;
}
.float-win.maximized {
  left: 1.5vw !important;
  top: 1.5dvh !important;
  width: 97vw !important;
  height: 94dvh !important;
}
.win-header {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 0.6rem;
  padding: 0.75rem 0.95rem;
  background: linear-gradient(135deg, rgba(14, 165, 233, 0.16), rgba(37, 99, 235, 0.1));
  border-bottom: 1px solid var(--border);
  cursor: move;
  user-select: none;
  touch-action: none;
}
.win-title { font-weight: 800; font-size: 0.98rem; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.win-controls { display: flex; align-items: center; gap: 0.3rem; flex-shrink: 0; }
.win-ctrl-btn {
  width: 30px;
  height: 30px;
  border-radius: 8px;
  border: 1px solid var(--border);
  background: var(--card);
  color: var(--text);
  cursor: pointer;
  display: flex;
  align-items: center;
  justify-content: center;
  font-size: 0.85rem;
}
.win-ctrl-btn:hover { border-color: var(--primary); }
.win-ctrl-btn.close:hover { background: var(--danger); color: #fff; border-color: var(--danger); }

.win-body {
  flex: 1;
  overflow: auto;
  padding: 1rem;
  -webkit-overflow-scrolling: touch;
}
.win-footer {
  padding: 0.7rem 1rem;
  border-top: 1px solid var(--border);
  display: flex;
  justify-content: flex-end;
  gap: 0.5rem;
  background: var(--bg-elev);
}
.win-resize-handle {
  position: absolute;
  right: 0;
  bottom: 0;
  width: 24px;
  height: 24px;
  cursor: nwse-resize;
  touch-action: none;
  display: flex;
  align-items: flex-end;
  justify-content: flex-end;
  padding: 3px;
  color: var(--muted);
  font-size: 0.8rem;
  user-select: none;
}

/* IMAGE ZOOM VIEWER INSIDE FLOATING WINDOW */
.img-zoom-toolbar {
  display: flex;
  align-items: center;
  justify-content: center;
  gap: 0.45rem;
  margin-bottom: 0.75rem;
  flex-wrap: wrap;
}
.img-zoom-stage {
  overflow: auto;
  border-radius: 12px;
  background: #ffffff;
  border: 1px solid var(--border);
  display: flex;
  align-items: center;
  justify-content: center;
  min-height: 260px;
  max-height: 68dvh;
  padding: 0.75rem;
}
.img-zoom-stage img {
  transition: transform 0.15s ease;
  transform-origin: center center;
  max-width: 100%;
  border-radius: 8px;
}

/* MOBILE BOTTOM NAV */
.mobile-nav {
  display: none;
  position: fixed;
  left: 0;
  right: 0;
  bottom: 0;
  z-index: 50;
  background: color-mix(in srgb, var(--bg-elev) 94%, transparent);
  backdrop-filter: blur(14px);
  border-top: 1px solid var(--border);
  padding: 0.35rem 0.5rem calc(0.35rem + env(safe-area-inset-bottom));
  justify-content: space-around;
}
.mob-btn {
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 0.1rem;
  border: none;
  background: transparent;
  color: var(--muted);
  font-size: 1.2rem;
  padding: 0.25rem 0.8rem;
  border-radius: 10px;
  cursor: pointer;
}
.mob-btn small { font-size: 0.72rem; font-weight: 700; }
.mob-btn.active { color: var(--primary); background: rgba(14, 165, 233, 0.12); }

/* TOAST */
.toast {
  position: fixed;
  left: 50%;
  bottom: calc(4.8rem + env(safe-area-inset-bottom));
  transform: translateX(-50%);
  z-index: 200;
  background: #0f172a;
  color: #f8fafc;
  padding: 0.7rem 1.15rem;
  border-radius: 12px;
  border: 1px solid rgba(14, 165, 233, 0.45);
  box-shadow: var(--shadow);
  font-size: 0.9rem;
  font-weight: 600;
}

/* RESPONSIVE */
@media (max-width: 860px) {
  .desktop-nav { display: none; }
  .mobile-nav { display: flex; }
  .topbar-sub { display: none; }
  .zoom-control [data-zoom-delta] { display: none; }
  .search-input { width: 100%; }
}
@media (max-width: 600px) {
  .form-row { grid-template-columns: 1fr; }
  .dose-list { grid-template-columns: 1fr; padding: 0.7rem; }
  .topbar { padding: calc(0.5rem + env(safe-area-inset-top)) 0.65rem 0.5rem; }
  .topbar-name { font-size: 0.92rem; }
  .float-win {
    left: 2vw !important;
    width: 96vw !important;
    max-height: 88dvh;
  }
}

@media print {
  .topbar, .mobile-nav, .hero-right, .section-actions, .take-btn, .windows-layer { display: none !important; }
  body { background: #fff; color: #000; }
}
