/* ============================================================
   Design tokens (dark theme only, per spec)
   ============================================================ */
:root {
  --bg:#101214; --surface:#191B1E; --surface-muted:#222528;
  --border:#2B2E32; --border-strong:#383B40;
  --ink:#F2F1ED; --ink-soft:#9A9DA4; --ink-faint:#757880;
  --accent:#5B93D1; --accent-soft:#1C2A3B;

  --radius-sm:10px; --radius-md:16px; --radius-lg:22px;
  --shadow-sm: 0 1px 2px rgba(0,0,0,.25), 0 1px 1px rgba(0,0,0,.18);
  --shadow-md: 0 8px 24px rgba(0,0,0,.35);
  --shadow-card: 0 1px 0 rgba(0,0,0,.15), 0 2px 10px rgba(0,0,0,.25);

  --font: -apple-system, BlinkMacSystemFont, "SF Pro Text", "Inter", "Segoe UI", Roboto, sans-serif;
}

* { box-sizing: border-box; }
html, body { height: 100%; }
html { background: var(--bg); }
body {
  margin: 0; background: var(--bg); color: var(--ink);
  font-family: var(--font); font-size: 14px; -webkit-font-smoothing: antialiased;
  overscroll-behavior-y: none; /* no white rubber-band flash / no accidental pull-to-refresh feel */
  -webkit-tap-highlight-color: transparent;
}
button, input, select, textarea { font-family: inherit; color: inherit; }
button { touch-action: manipulation; } /* removes the ~300ms ghost-tap delay */
::selection { background: var(--accent-soft); color: var(--ink); }

/* Scrollbar */
* { scrollbar-width: thin; scrollbar-color: var(--border-strong) transparent; }
*::-webkit-scrollbar { width: 8px; height: 8px; }
*::-webkit-scrollbar-thumb { background: var(--border-strong); border-radius: 8px; }
*::-webkit-scrollbar-track { background: transparent; }

a { color: inherit; }
[hidden] { display: none !important; }

:focus-visible { outline: 2px solid var(--accent); outline-offset: 2px; border-radius: 4px; }

@media (prefers-reduced-motion: reduce) {
  * { animation-duration: .001ms !important; transition-duration: .001ms !important; }
}

.tabnums { font-variant-numeric: tabular-nums; }

/* ============================================================
   Buttons / inputs / toggles / segmented controls
   ============================================================ */
.btn {
  border: none; cursor: pointer; border-radius: 999px; padding: 10px 18px;
  font-size: 13.5px; font-weight: 600; transition: transform .15s ease, background .15s ease;
}
.btn:active { transform: scale(.97); }
.btn-primary { background: var(--accent); color: #fff; }
.btn-primary:hover { background: #6ba0da; }
.btn-secondary { background: var(--surface-muted); color: var(--ink); }
.btn-secondary:hover { background: var(--border); }
.btn-danger { background: #2F1B1E; color: #E27884; }
.btn-danger:hover { background: #3a2226; }
.btn-icon {
  border: none; background: transparent; cursor: pointer; color: var(--ink-soft);
  width: 34px; height: 34px; border-radius: 999px; display: inline-flex; align-items: center; justify-content: center;
  font-size: 16px; transition: background .15s ease, color .15s ease;
}
.btn-icon:hover { background: var(--surface-muted); color: var(--ink); }
.btn-block { width: 100%; }

.field { display: flex; flex-direction: column; gap: 6px; margin-bottom: 14px; }
.field label { font-size: .7rem; text-transform: uppercase; letter-spacing: .05em; font-weight: 700; color: var(--ink-soft); }
.field input[type="text"], .field input[type="password"], .field input[type="date"],
.field input[type="time"], .field input[type="number"], .field select, .field textarea {
  background: var(--surface); border: 1px solid var(--border); border-radius: var(--radius-sm);
  padding: 10px 12px; color: var(--ink); font-size: 16px; outline: none; transition: border-color .15s ease, box-shadow .15s ease;
  /* 16px is the iOS Safari threshold below which focusing an input auto-zooms the page */
}
.field textarea { resize: vertical; min-height: 64px; font-family: inherit; }
.field input:focus, .field select:focus, .field textarea:focus {
  border-color: var(--accent); box-shadow: 0 0 0 3px var(--accent-soft);
}
.field-row { display: grid; grid-template-columns: 1fr 1fr; gap: 10px; }

.switch { position: relative; display: inline-block; width: 42px; height: 25px; flex: none; }
.switch input { opacity: 0; width: 0; height: 0; }
.switch .track {
  position: absolute; inset: 0; background: var(--surface-muted); border: 1px solid var(--border);
  border-radius: 999px; cursor: pointer; transition: background .15s ease;
}
.switch .track::before {
  content: ""; position: absolute; height: 19px; width: 19px; left: 2px; top: 2px;
  background: var(--ink-soft); border-radius: 50%; transition: transform .15s ease, background .15s ease;
}
.switch input:checked + .track { background: var(--accent); border-color: var(--accent); }
.switch input:checked + .track::before { transform: translateX(17px); background: #fff; }

.segmented { display: inline-flex; background: var(--surface-muted); border-radius: 999px; padding: 3px; gap: 2px; }
.segmented button {
  border: none; background: transparent; color: var(--ink-soft); padding: 7px 14px; border-radius: 999px;
  font-size: 13px; font-weight: 600; cursor: pointer; transition: background .15s ease, color .15s ease;
}
.segmented button.active { background: var(--surface); color: var(--ink); box-shadow: var(--shadow-sm); }

.color-swatch { width: 16px; height: 16px; border-radius: 5px; flex: none; display: inline-block; }
.color-picker { display: flex; flex-wrap: wrap; gap: 8px; }
.color-picker button {
  width: 26px; height: 26px; border-radius: 8px; border: 2px solid transparent; cursor: pointer;
}
.color-picker button.active { border-color: var(--ink); }

/* ============================================================
   Auth screens
   ============================================================ */
.auth-screen {
  min-height: 100dvh; display: flex; align-items: center; justify-content: center; padding: 24px;
  padding-top: max(24px, env(safe-area-inset-top)); padding-bottom: max(24px, env(safe-area-inset-bottom));
  background: radial-gradient(circle at 20% 0%, #16233a 0%, var(--bg) 55%);
}
.auth-card {
  width: 100%; max-width: 360px; background: var(--surface); border: 1px solid var(--border);
  border-radius: var(--radius-lg); padding: 32px 28px; box-shadow: var(--shadow-md);
}
.auth-logo { display: flex; align-items: center; gap: 10px; margin-bottom: 22px; }
.auth-logo .mark {
  width: 34px; height: 34px; border-radius: 10px; background: linear-gradient(155deg, var(--accent), #2c5da3);
  display: flex; align-items: center; justify-content: center; font-weight: 700; color: #fff; font-size: 15px;
}
.auth-logo .name { font-weight: 700; font-size: 17px; }
.auth-card h1 { font-size: 19px; margin: 0 0 4px; }
.auth-card .sub { color: var(--ink-soft); font-size: 13px; margin: 0 0 20px; }
.auth-error {
  background: #2F1B1E; color: #E27884; border-radius: var(--radius-sm); padding: 10px 12px;
  font-size: 13px; margin-bottom: 14px;
}
.auth-switch { text-align: center; margin-top: 16px; font-size: 13px; color: var(--ink-soft); }
.auth-switch button { background: none; border: none; color: var(--accent); font-weight: 600; cursor: pointer; font-size: 13px; padding: 0; }

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

.sidebar {
  width: 232px; flex: none; background: var(--surface); border-right: 1px solid var(--border);
  display: flex; flex-direction: column; padding: 18px 16px; gap: 20px;
}
.sidebar .brand { display: flex; align-items: center; gap: 10px; padding: 0 4px; }
.sidebar .brand .mark {
  width: 30px; height: 30px; border-radius: 9px; background: linear-gradient(155deg, var(--accent), #2c5da3);
  display: flex; align-items: center; justify-content: center; font-weight: 700; color: #fff; font-size: 13px;
}
.sidebar .brand .name { font-weight: 700; font-size: 15px; }

.new-event-btn { display: flex; align-items: center; justify-content: center; gap: 6px; }

.fab {
  display: none; position: fixed; z-index: 60;
  right: max(20px, calc(env(safe-area-inset-right) + 16px));
  bottom: max(20px, calc(env(safe-area-inset-bottom) + 16px));
  width: 56px; height: 56px; border-radius: 999px; border: none;
  background: var(--accent); color: #fff; font-size: 28px; line-height: 1; font-weight: 400;
  align-items: center; justify-content: center; box-shadow: var(--shadow-md); cursor: pointer;
  transition: transform .12s ease, background .15s ease;
}
.fab:hover { background: #6ba0da; }
.fab:active { transform: scale(.93); }

.mini-cal { padding: 2px; }
.mini-cal-head { display: flex; align-items: center; justify-content: space-between; margin-bottom: 8px; }
.mini-cal-head .label { font-size: 12.5px; font-weight: 700; }
.mini-cal-grid { display: grid; grid-template-columns: repeat(7, 1fr); gap: 2px; text-align: center; }
.mini-cal-grid .dow { font-size: 10px; color: var(--ink-faint); padding-bottom: 4px; }
.mini-cal-grid .day {
  font-size: 11.5px; padding: 5px 0; border-radius: 7px; cursor: pointer; color: var(--ink-soft);
}
.mini-cal-grid .day:hover { background: var(--surface-muted); }
.mini-cal-grid .day.muted { color: var(--ink-faint); opacity: .4; }
.mini-cal-grid .day.today { color: var(--accent); font-weight: 700; }
.mini-cal-grid .day.selected { background: var(--accent); color: #fff; font-weight: 700; }

.side-section { display: flex; flex-direction: column; gap: 6px; }
.side-section .side-head {
  display: flex; align-items: center; justify-content: space-between; padding: 0 4px 4px;
  font-size: .68rem; text-transform: uppercase; letter-spacing: .05em; font-weight: 700; color: var(--ink-faint);
}
.cal-row { display: flex; align-items: center; gap: 8px; padding: 7px 6px; border-radius: 8px; cursor: pointer; }
.cal-row:hover { background: var(--surface-muted); }
.cal-row .cb { flex: none; }
.cal-row .cb input { display: none; }
.cal-row .cb .box {
  width: 15px; height: 15px; border-radius: 5px; border: 2px solid var(--border-strong);
  display: inline-flex; align-items: center; justify-content: center; font-size: 10px; color: #fff;
}
.cal-row .cb input:checked + .box { border-color: transparent; }
.cal-row .label { flex: 1; font-size: 13px; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.cal-row .edit-btn { opacity: 0; font-size: 12px; }
.cal-row:hover .edit-btn { opacity: 1; }

.sidebar-bottom { margin-top: auto; display: flex; flex-direction: column; gap: 4px; }
.user-row { display: flex; align-items: center; gap: 8px; padding: 8px 6px; border-radius: 10px; }
.user-row .avatar {
  width: 26px; height: 26px; border-radius: 999px; background: var(--surface-muted); color: var(--ink-soft);
  display: flex; align-items: center; justify-content: center; font-size: 12px; font-weight: 700; flex: none;
}
.user-row .uname { font-size: 13px; font-weight: 600; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; flex: 1; }
.sidebar-link {
  display: flex; align-items: center; gap: 8px; padding: 8px 6px; border-radius: 8px; background: none;
  border: none; color: var(--ink-soft); font-size: 13px; cursor: pointer; text-align: start; width: 100%;
}
.sidebar-link:hover { background: var(--surface-muted); color: var(--ink); }

/* ============================================================
   Main calendar area
   ============================================================ */
.main { flex: 1; display: flex; flex-direction: column; min-width: 0; }

.topbar {
  display: flex; align-items: center; gap: 14px; padding: 14px 22px; border-bottom: 1px solid var(--border);
  flex-wrap: wrap; padding-top: max(14px, env(safe-area-inset-top));
}
.topbar .nav-cluster { display: flex; align-items: center; gap: 4px; }
.topbar .period-label { font-size: 18px; font-weight: 700; min-width: 160px; }
.topbar .spacer { flex: 1; }

.view-body { flex: 1; overflow: auto; padding: 16px 22px 40px; }

/* Month grid */
.month-grid { display: grid; grid-template-columns: repeat(7, 1fr); border: 1px solid var(--border); border-radius: var(--radius-md); overflow: hidden; }
.month-grid .dow-row { display: contents; }
.month-grid .dow {
  background: var(--surface); padding: 8px 10px; font-size: .68rem; text-transform: uppercase;
  letter-spacing: .05em; font-weight: 700; color: var(--ink-faint); border-bottom: 1px solid var(--border);
}
.month-cell {
  background: var(--bg); border-right: 1px solid var(--border); border-bottom: 1px solid var(--border);
  min-height: 108px; padding: 6px; display: flex; flex-direction: column; gap: 3px; cursor: pointer;
  transition: background .12s ease;
}
.month-cell:hover { background: var(--surface-muted); }
.month-cell.muted { opacity: .38; }
.month-cell .daynum {
  font-size: 12.5px; font-weight: 600; color: var(--ink-soft); width: 22px; height: 22px; border-radius: 50%;
  display: flex; align-items: center; justify-content: center;
}
.month-cell.today .daynum { background: var(--accent); color: #fff; }
.month-cell .events { display: flex; flex-direction: column; gap: 2px; overflow: hidden; }
.event-pill {
  font-size: 11px; padding: 2px 6px; border-radius: 6px; white-space: nowrap; overflow: hidden;
  text-overflow: ellipsis; color: #fff; cursor: pointer; font-weight: 600;
}
.event-pill .t { opacity: .85; font-weight: 500; }
.more-link { font-size: 10.5px; color: var(--ink-faint); padding: 2px 6px; cursor: pointer; }

/* Week / day grid */
.time-grid-wrap { display: flex; border: 1px solid var(--border); border-radius: var(--radius-md); overflow: hidden; }
.time-gutter { width: 56px; flex: none; background: var(--surface); border-right: 1px solid var(--border); }
.time-gutter .hour-label { height: 52px; font-size: 10.5px; color: var(--ink-faint); text-align: right; padding-right: 8px; transform: translateY(-6px); }
.time-cols { flex: 1; display: grid; }
.time-col { position: relative; border-right: 1px solid var(--border); background: var(--bg); }
.time-col:last-child { border-right: none; }
.time-col .hour-line { height: 52px; border-bottom: 1px solid var(--border); }
.time-col-head { padding: 8px 6px; text-align: center; border-bottom: 1px solid var(--border); background: var(--surface); position: sticky; top: 0; }
.time-col-head .dname { font-size: .65rem; text-transform: uppercase; color: var(--ink-faint); letter-spacing: .04em; }
.time-col-head .dnum { font-size: 15px; font-weight: 700; margin-top: 2px; }
.time-col-head.today .dnum { color: var(--accent); }
.time-cols-scroll { display: flex; flex-direction: column; }
.time-block {
  position: absolute; left: 3px; right: 3px; border-radius: 7px; padding: 4px 6px; color: #fff;
  font-size: 11px; font-weight: 600; overflow: hidden; cursor: grab; box-shadow: var(--shadow-sm);
  user-select: none; touch-action: none;
}
.time-block.dragging { opacity: .85; cursor: grabbing; box-shadow: var(--shadow-md); z-index: 5; }
.time-block-resize { position: absolute; left: 0; right: 0; bottom: 0; height: 6px; cursor: ns-resize; }
.allday-row { display: flex; border-bottom: 1px solid var(--border); }
.allday-row .time-gutter-spacer { width: 56px; flex: none; border-right: 1px solid var(--border); background: var(--surface); }
.allday-row .allday-cols { flex: 1; display: grid; }
.allday-cols .allday-cell { border-right: 1px solid var(--border); padding: 4px; display: flex; flex-direction: column; gap: 2px; min-height: 30px; }

/* Custom date-picker popover (used instead of the native, always-Gregorian
   <input type="date"> so the Jalali calendar system is respected everywhere) */
.date-popover {
  background: var(--surface); border: 1px solid var(--border); border-radius: var(--radius-md);
  box-shadow: var(--shadow-md); padding: 10px; width: 240px; z-index: 150;
}
.date-popover-head { display: flex; align-items: center; justify-content: space-between; margin-bottom: 8px; font-size: 12.5px; font-weight: 700; }
.date-popover-head button { border: none; background: transparent; color: var(--ink-soft); cursor: pointer; font-size: 16px; padding: 2px 8px; border-radius: 6px; }
.date-popover-head button:hover { background: var(--surface-muted); color: var(--ink); }
.date-popover-grid { display: grid; grid-template-columns: repeat(7, 1fr); gap: 2px; text-align: center; }
.date-popover-grid .dow { font-size: 10px; color: var(--ink-faint); padding-bottom: 4px; }
.date-popover-day {
  font-size: 11.5px; padding: 6px 0; border-radius: 7px; cursor: pointer; color: var(--ink-soft);
  border: none; background: transparent;
}
.date-popover-day:hover { background: var(--surface-muted); }
.date-popover-day.muted { color: var(--ink-faint); opacity: .4; }
.date-popover-day.today { color: var(--accent); font-weight: 700; }
.date-popover-day.selected { background: var(--accent); color: #fff; font-weight: 700; }

/* Agenda / list view */
.agenda-group { margin-bottom: 18px; }
.agenda-group .agenda-date { font-size: .7rem; text-transform: uppercase; letter-spacing: .05em; font-weight: 700; color: var(--ink-faint); margin-bottom: 8px; }
.agenda-item { display: flex; align-items: center; gap: 10px; padding: 10px; border-radius: var(--radius-sm); background: var(--surface); border: 1px solid var(--border); margin-bottom: 6px; cursor: pointer; }
.agenda-item:hover { border-color: var(--border-strong); }
.agenda-item .dot { width: 9px; height: 9px; border-radius: 50%; flex: none; }
.agenda-item .title { font-weight: 600; flex: 1; }
.agenda-item .time { font-size: 12px; color: var(--ink-soft); }

.empty-state { text-align: center; color: var(--ink-faint); padding: 60px 20px; font-size: 13px; }

/* ============================================================
   Modals
   ============================================================ */
.modal-overlay {
  position: fixed; inset: 0; background: rgba(0,0,0,.5); display: flex; align-items: center; justify-content: center;
  padding: 20px; z-index: 100;
}
.modal {
  width: 100%; max-width: 420px; background: var(--surface); border: 1px solid var(--border); border-radius: var(--radius-md);
  box-shadow: var(--shadow-md); max-height: 90vh; overflow-y: auto;
}
.modal-head { display: flex; align-items: center; justify-content: space-between; padding: 16px 18px; border-bottom: 1px solid var(--border); }
.modal-head h2 { font-size: 15.5px; margin: 0; }
.modal-body { padding: 18px; }
.modal-foot { display: flex; align-items: center; justify-content: space-between; gap: 8px; padding: 14px 18px; border-top: 1px solid var(--border); }
.modal-foot .right { display: flex; gap: 8px; }

.settings-section { margin-bottom: 20px; }
.settings-section h3 { font-size: .7rem; text-transform: uppercase; letter-spacing: .05em; color: var(--ink-faint); margin: 0 0 10px; }
.settings-row { display: flex; align-items: center; justify-content: space-between; padding: 10px 0; border-bottom: 1px solid var(--border); gap: 12px; }
.settings-row:last-child { border-bottom: none; }
.settings-row .lbl { font-size: 13.5px; }
.settings-row .lbl .sub { display: block; font-size: 11.5px; color: var(--ink-faint); margin-top: 2px; }

/* Toast */
.toast {
  position: fixed; left: 50%; transform: translateX(-50%); background: #111; color: #fff;
  bottom: max(20px, calc(env(safe-area-inset-bottom) + 16px));
  padding: 10px 18px; border-radius: 999px; font-size: 13px; box-shadow: var(--shadow-md); z-index: 200;
  opacity: 0; transition: opacity .2s ease;
}
.toast.show { opacity: 1; }

/* ============================================================
   Mobile (iPhone web-app)
   ============================================================ */
.mobile-topbar { display: none; }
#btn-menu-toggle { display: none; }
@media (max-width: 860px) {
  .fab { display: flex; }
  /* The sidebar's own "+ New Event" button is redundant once the FAB is
     visible, and re-showing the drawer just to start an event added an
     extra tap — the FAB is the primary entry point on mobile now. */
  .new-event-btn { display: none; }

  #btn-menu-toggle { display: inline-flex; width: 44px; height: 44px; }
  .sidebar {
    position: fixed; inset: 0 20% 0 auto; z-index: 90; transform: translateX(100%);
    transition: transform .2s ease; width: 78vw; max-width: 300px;
    padding-top: max(18px, env(safe-area-inset-top));
    padding-bottom: max(18px, env(safe-area-inset-bottom));
  }
  .sidebar.open { transform: translateX(0); }
  .sidebar-scrim { display: none; position: fixed; inset: 0; background: rgba(0,0,0,.4); z-index: 80; }
  .sidebar-scrim.show { display: block; }

  .topbar { padding: 10px 12px; padding-top: max(10px, env(safe-area-inset-top)); gap: 8px; }
  .topbar .period-label { font-size: 15px; min-width: 0; }
  .month-cell { min-height: 76px; }
  .view-body { padding: 10px 10px calc(30px + 72px); } /* extra bottom room so the FAB never covers the last row */

  /* Touch targets: Apple HIG recommends ~44pt minimum. */
  .btn-icon { width: 44px; height: 44px; font-size: 18px; }
  .mini-cal-grid .day { padding: 9px 0; }
  .date-popover-day { padding: 10px 0; }
  .color-picker button { width: 34px; height: 34px; }

  /* Modals become bottom sheets — the native iOS pattern — instead of a
     centered card, and respect the home-indicator safe area. */
  .modal-overlay { align-items: flex-end; padding: 0; }
  .modal {
    max-width: 100%; width: 100%; max-height: 88dvh; border-radius: 20px 20px 0 0;
    padding-bottom: env(safe-area-inset-bottom);
  }
  .modal::before {
    content: ""; display: block; width: 36px; height: 4px; border-radius: 999px;
    background: var(--border-strong); margin: 10px auto 0;
  }

  /* The date-picker popover anchors under its input on desktop, but on a
     narrow screen that can run off either edge — pin it to a centered
     sheet near the bottom instead. Both the JS-set position and the
     app's own inline top/left are overridden here (this rule needs
     !important to beat inline styles set by DatePicker.open()). */
  .date-popover {
    position: fixed !important; top: auto !important; left: 50% !important;
    bottom: max(16px, calc(env(safe-area-inset-bottom) + 16px)) !important;
    transform: translateX(-50%); width: min(320px, calc(100vw - 32px));
  }

  /* Week view: 7 narrow columns don't fit an iPhone width, so let the
     grid keep a legible minimum column width and scroll horizontally
     instead of squeezing every day unreadably thin. The hour gutter
     stays pinned via position: sticky while the days scroll under it. */
  .time-grid-wrap { overflow-x: auto; -webkit-overflow-scrolling: touch; }
  .time-gutter { position: sticky; left: 0; z-index: 2; }
  .allday-row { overflow-x: auto; -webkit-overflow-scrolling: touch; }
  .allday-row .time-gutter-spacer { position: sticky; left: 0; z-index: 2; }
}
