:root {
  --bg: #0f1220;
  --panel: #171b2e;
  --panel-alt: #1e2338;
  --border: #2a2f47;
  --text: #e7e9f5;
  --text-dim: #9aa1c0;
  --accent: #5b8cff;
  --ok: #3ecf8e;
  --warn: #f2b544;
  --err: #ef5c73;
  --cool: #7dd3fc;
  font-family: -apple-system, "Segoe UI", Roboto, Helvetica, Arial, sans-serif;
}

* { box-sizing: border-box; }

body {
  margin: 0;
  background: var(--bg);
  color: var(--text);
  font-size: 14px;
}

.hidden { display: none !important; }

.screen { min-height: 100vh; }

/* --- login --- */
#login-screen { display: flex; align-items: center; justify-content: center; }
.login-card {
  background: var(--panel);
  border: 1px solid var(--border);
  border-radius: 12px;
  padding: 32px 40px;
  text-align: center;
  max-width: 420px;
}
.brand-logo { width: 28px; height: 28px; border-radius: 7px; vertical-align: middle; display: inline-block; }
.brand-logo-lg { width: 72px; height: 72px; border-radius: 18px; margin-bottom: 12px; }
.brand { display: inline-flex; align-items: center; gap: 8px; }
.login-card h1 { margin: 0 0 12px; font-size: 20px; }
.login-card p { color: var(--text-dim); }
.login-form { text-align: left; margin-top: 8px; }
.login-form .form-row input { width: 100%; }
.login-submit { width: 100%; margin-top: 4px; }
.login-error { color: var(--err); font-size: 13px; margin: 4px 0 8px; }

/* --- topbar --- */
.topbar {
  display: flex;
  align-items: center;
  justify-content: space-between;
  flex-wrap: wrap;
  row-gap: 8px;
  padding: 10px 18px;
  background: var(--panel);
  border-bottom: 1px solid var(--border);
}
.topbar-left, .topbar-right { display: flex; align-items: center; gap: 14px; flex-wrap: wrap; row-gap: 6px; }
.brand { font-weight: 600; white-space: nowrap; }
.nav-toggle-btn { display: none; font-size: 16px; line-height: 1; padding: 6px 10px; }
.mode-banner {
  font-size: 12px;
  padding: 3px 10px;
  border-radius: 999px;
  font-weight: 600;
  letter-spacing: 0.03em;
  white-space: nowrap;
}
.mode-banner.read-only { background: rgba(242,181,68,0.15); color: var(--warn); }
.mode-banner.live { background: rgba(239,92,115,0.15); color: var(--err); }

.live-status {
  font-size: 11px;
  padding: 3px 9px;
  border-radius: 999px;
  border: 1px solid var(--border);
  color: var(--text-dim);
}
.live-status.connected { color: var(--ok); border-color: var(--ok); }
.live-status.reconnecting, .live-status.connecting { color: var(--warn); border-color: var(--warn); }
.live-status.offline { color: var(--err); border-color: var(--err); }

/* --- banners / toasts --- */
.banner {
  padding: 10px 18px;
  font-size: 13px;
}
.banner-warning { background: rgba(242,181,68,0.15); color: var(--warn); border-bottom: 1px solid var(--border); }
.banner-error { background: rgba(239,92,115,0.15); color: var(--err); }

.toast-root {
  position: fixed; bottom: 16px; right: 16px;
  display: flex; flex-direction: column; gap: 8px;
  z-index: 1000;
}
.toast {
  background: var(--panel-alt);
  border: 1px solid var(--border);
  border-radius: 8px;
  padding: 10px 14px;
  max-width: 340px;
  font-size: 13px;
}
.toast.error { border-color: var(--err); }
.toast.success { border-color: var(--ok); }

/* --- layout --- */
.layout { display: flex; min-height: calc(100vh - 45px); }
.sidenav {
  width: 180px;
  background: var(--panel);
  border-right: 1px solid var(--border);
  padding: 14px 0;
  flex-shrink: 0;
}
.sidenav a {
  display: block;
  padding: 9px 20px;
  color: var(--text-dim);
  text-decoration: none;
  font-size: 13px;
}
.sidenav a:hover { color: var(--text); background: var(--panel-alt); }
.sidenav a.active { color: var(--text); background: var(--panel-alt); border-left: 3px solid var(--accent); padding-left: 17px; }
/* Desktop already has a topbar Log out button -- the drawer's own entry
   point only matters (and only shows) at mobile widths; see the mobile
   section below. Selector uses the id (not just .sidenav-logout) because
   ".sidenav a" above is MORE specific than a single class and would
   otherwise force display:block regardless of this rule or the media
   query's own override. */
#sidenav-logout { display: none; }
.sidenav-install-btn {
  display: block; width: calc(100% - 20px); margin: 6px 10px 0;
  text-align: left; padding: 9px 10px; font-size: 13px; border-radius: 6px;
}

.nav-backdrop {
  position: fixed; inset: 0;
  background: rgba(0,0,0,0.5);
  z-index: 400;
}

.content { flex: 1; padding: 20px; overflow-x: auto; }

/* --- generic widgets --- */
.card {
  background: var(--panel);
  border: 1px solid var(--border);
  border-radius: 10px;
  padding: 16px;
  margin-bottom: 16px;
}
.stat-grid { display: flex; flex-wrap: wrap; gap: 12px; margin-bottom: 16px; }
.stat {
  background: var(--panel);
  border: 1px solid var(--border);
  border-radius: 10px;
  padding: 12px 18px;
  min-width: 110px;
}
.stat .n { font-size: 22px; font-weight: 700; }
.stat .l { font-size: 11px; color: var(--text-dim); text-transform: uppercase; letter-spacing: 0.04em; }

.btn {
  background: var(--panel-alt);
  color: var(--text);
  border: 1px solid var(--border);
  border-radius: 6px;
  padding: 6px 14px;
  font-size: 13px;
  cursor: pointer;
  min-height: 32px;
  touch-action: manipulation;
}
.btn:hover:not(:disabled) { border-color: var(--accent); }
.btn:disabled { opacity: 0.5; cursor: not-allowed; }
.btn-primary { background: var(--accent); border-color: var(--accent); color: white; }
.btn-danger { background: rgba(239,92,115,0.15); border-color: var(--err); color: var(--err); }
.btn-ghost { background: transparent; }
.btn-sm { padding: 3px 9px; font-size: 12px; }

table { width: 100%; border-collapse: collapse; font-size: 13px; }
th, td { text-align: left; padding: 7px 10px; border-bottom: 1px solid var(--border); }
th { color: var(--text-dim); font-weight: 500; font-size: 11px; text-transform: uppercase; letter-spacing: 0.03em; }
tr.dragging { opacity: 0.4; }
tr[draggable="true"] { cursor: grab; }

.badge {
  display: inline-block;
  padding: 2px 8px;
  border-radius: 999px;
  font-size: 11px;
  font-weight: 600;
}
.badge-running { background: rgba(62,207,142,0.15); color: var(--ok); }
.badge-stopped { background: rgba(154,161,192,0.15); color: var(--text-dim); }
.badge-stopping { background: rgba(242,181,68,0.15); color: var(--warn); }
.badge-error { background: rgba(239,92,115,0.15); color: var(--err); }
.badge-cooldown { background: rgba(125,211,252,0.15); color: var(--cool); }
.badge-target { background: rgba(91,140,255,0.15); color: var(--accent); }
.badge-confirmed { background: rgba(62,207,142,0.15); color: var(--ok); }
.badge-cancelled { background: rgba(239,92,115,0.15); color: var(--err); }
.badge-expired { background: rgba(154,161,192,0.15); color: var(--text-dim); }

.form-row { margin-bottom: 10px; display: flex; flex-direction: column; gap: 4px; }
.form-row label { font-size: 12px; color: var(--text-dim); }
.form-row input, .form-row select {
  background: var(--panel-alt);
  border: 1px solid var(--border);
  color: var(--text);
  padding: 6px 8px;
  border-radius: 6px;
  font-size: 13px;
}

/* Two-or-more inputs side by side inside a .form-row (e.g. warehouse
   ID+name, exact time-range select+refresh button, manual start/end
   time) -- a bare display:flex row does not shrink text/number inputs
   below their intrinsic size, which overflowed a 360px viewport (Phase
   15 mobile check). flex:1 + min-width:0 lets every child shrink to fit. */
.inline-fields { display: flex; gap: 6px; min-width: 0; }
.inline-fields > * { flex: 1 1 0; min-width: 0; }

.modal-backdrop {
  position: fixed; inset: 0; background: rgba(0,0,0,0.55);
  display: flex; align-items: flex-start; justify-content: center;
  padding-top: 60px; z-index: 500;
}
.modal {
  background: var(--panel);
  border: 1px solid var(--border);
  border-radius: 10px;
  padding: 20px;
  width: 420px;
  max-width: 92vw;
  max-height: 80vh;
  overflow-y: auto;
}
.modal h2 { margin-top: 0; font-size: 16px; }
.modal-actions { display: flex; justify-content: flex-end; gap: 8px; margin-top: 16px; }

.muted { color: var(--text-dim); }
.mono { font-family: ui-monospace, SFMono-Regular, Menlo, Consolas, monospace; font-size: 12px; overflow-wrap: anywhere; }
/* FINAL SHIP Part B -- the account ID must never wrap onto multiple lines
   in the desktop accounts table; every other .mono use (task ids in
   narrower contexts) keeps the default wrapping behavior above. */
.account-id-cell { white-space: nowrap; min-width: 12ch; }
.section-title { font-size: 15px; font-weight: 600; margin: 0 0 10px; }
.filters { display: flex; gap: 10px; margin-bottom: 12px; flex-wrap: wrap; }
.filters select, .filters input { background: var(--panel-alt); color: var(--text); border: 1px solid var(--border); border-radius: 6px; padding: 5px 8px; font-size: 12px; }

/* --- card layout (Phase 14.1): the mobile-width alternative to a table,
   built from the exact same data the desktop <table> renders from. Both
   markups exist in the DOM; only one is ever visible at a time, chosen
   purely by the media query below -- no separate fetch/state/API path. --- */
.mobile-cards { display: none; }
.entity-card {
  background: var(--panel);
  border: 1px solid var(--border);
  border-radius: 10px;
  padding: 12px 14px;
  margin-bottom: 10px;
}
.entity-card-header { display: flex; justify-content: space-between; align-items: flex-start; gap: 8px; margin-bottom: 8px; }
.entity-card-title { font-weight: 600; font-size: 14px; }
.entity-card-subtitle { font-size: 12px; color: var(--text-dim); }
.entity-card-row { display: flex; justify-content: space-between; flex-wrap: wrap; gap: 4px 10px; font-size: 13px; padding: 3px 0; }
.entity-card-row .k { color: var(--text-dim); }
.entity-card-actions { display: flex; flex-wrap: wrap; gap: 6px; margin-top: 10px; }
.entity-card-badges { display: flex; flex-wrap: wrap; gap: 6px; }

/* Phase 17 — onboarding: toolbar above a list, an empty-state placeholder
   when there is nothing to show yet, and one row per accounts.json import
   candidate. Reuses .card/.btn/.badge-* rather than inventing new visual
   language. */
.toolbar-row { display: flex; justify-content: flex-end; gap: 8px; flex-wrap: wrap; margin-bottom: 10px; }
.empty-state { text-align: center; padding: 28px 16px; color: var(--text-dim); }
.empty-state .btn { margin-top: 12px; }
.import-candidate-row {
  display: flex; justify-content: space-between; align-items: center;
  gap: 8px; flex-wrap: wrap; padding: 8px 0; border-bottom: 1px solid var(--border);
}
.import-candidate-row:last-child { border-bottom: none; }

/* HOTFIX 17.2 — access-link display: input + copy button wrap onto their
   own line at narrow widths rather than overflowing (section 22: 360px/
   390px/430px, no horizontal overflow, touch-friendly copy button). */
.access-link-row { display: flex; gap: 6px; flex-wrap: wrap; }
.access-link-row .access-link-input { flex: 1 1 200px; min-width: 0; }
.access-link-row .btn { flex: 0 0 auto; min-width: 110px; }

/* Checkbox + label pairs (Phase 16 Telegram preferences) -- deliberately
   NOT .entity-card-row, whose justify-content:space-between is meant for
   key/value pairs and pushes a checkbox to one edge and its label to the
   other on a wide row. This keeps them adjacent, as a checkbox is expected
   to read. */
.checkbox-row { display: flex; align-items: center; gap: 8px; font-size: 13px; padding: 4px 0; }

.priority-controls { display: inline-flex; gap: 4px; }
.priority-controls .btn { padding: 4px 9px; min-width: 32px; }

/* Native <details> is the "collapsed by default, expandable by tap"
   mechanism for secondary technical fields (account details) and long
   audit/journal before-after content -- no custom JS state needed. */
details.collapsible summary {
  cursor: pointer;
  color: var(--text-dim);
  font-size: 12px;
  padding: 4px 0;
  touch-action: manipulation;
}
details.collapsible[open] summary { color: var(--text); }
details.collapsible .collapsible-body { padding-top: 6px; }
details.collapsible pre {
  white-space: pre-wrap;
  word-break: break-word;
  margin: 0;
  font-size: 12px;
}

/* =========================================================================
   MOBILE (Phase 14.1) -- phones from ~360px through small tablets. Fluid
   (%/flex based), not tuned to one exact device width, so it holds up
   across 360/390/430 without separate breakpoints per device.
   ========================================================================= */
@media (max-width: 760px) {
  body { font-size: 15px; } /* comfortable phone reading size, still fits 360px with this layout */

  .nav-toggle-btn { display: inline-block; }

  .layout { flex-direction: column; position: relative; }

  /* Off-canvas drawer, not a permanently-inaccessible off-screen sidebar:
     it is always reachable via the hamburger button and closes on
     backdrop tap or route selection (see main.js). */
  .sidenav {
    position: fixed;
    top: 0; bottom: 0; left: 0;
    width: 240px;
    max-width: 82vw;
    z-index: 450;
    display: flex;
    flex-direction: column;
    padding: 16px 0;
    transform: translateX(-100%);
    transition: transform 0.18s ease-out;
    overflow-y: auto;
  }
  .sidenav.open { transform: translateX(0); }
  .sidenav a { padding: 12px 20px; font-size: 14px; }
  .sidenav a.active { border-left: 3px solid var(--accent); padding-left: 17px; }
  #sidenav-logout {
    display: block;
    margin-top: auto;
    border-top: 1px solid var(--border);
    color: var(--err) !important;
  }

  .content { padding: 12px; overflow-x: hidden; }

  /* Desktop tables give way to stacked cards built from the same data. */
  table.desktop-table { display: none; }
  .mobile-cards { display: block; }

  .stat-grid { gap: 8px; }
  .stat { flex: 1 1 calc(50% - 8px); min-width: 0; padding: 10px 12px; }

  .modal-backdrop { padding-top: 0; align-items: stretch; }
  .modal {
    width: 100%;
    max-width: 100%;
    max-height: 100vh;
    height: 100%;
    border-radius: 0;
    display: flex;
    flex-direction: column;
  }
  .modal-actions { position: sticky; bottom: 0; background: var(--panel); padding-top: 10px; margin-top: auto; }

  .filters { flex-direction: column; }
  .filters select, .filters input { width: 100%; }

  .toast-root { left: 12px; right: 12px; bottom: 12px; }
  .toast { max-width: none; }

  .login-card { padding: 24px 20px; width: 100%; max-width: 360px; }

  /* Comfortable tap targets throughout (section 6) -- buttons/links get a
     touch-friendly minimum height on phones specifically, without
     changing desktop's denser sizing. */
  .btn { min-height: 38px; padding: 8px 14px; }
  .btn-sm { min-height: 34px; padding: 6px 12px; }
  .sidenav a { min-height: 44px; display: flex; align-items: center; }
}

/* --- Phase 15: real month calendar ---------------------------------------
   Desktop and mobile share the exact same table markup/data (section 21) --
   only cell sizing/legend layout changes at narrow widths, never the
   underlying date logic (that all lives in calendar_helpers.js). */
.modal-calendar { width: 640px; }
.calendar-table { width: 100%; border-collapse: collapse; table-layout: fixed; }
.calendar-table th {
  font-size: 11px; color: var(--text-dim); font-weight: 500; padding: 4px 2px; text-align: center;
}
.calendar-cell {
  border: 1px solid var(--border);
  text-align: center;
  vertical-align: middle;
  padding: 6px 2px;
  height: 52px;
}
.calendar-cell-blank { border-color: transparent; }
.calendar-cell-day { font-size: 12px; color: var(--text-dim); }
.calendar-cell-glyph { font-size: 15px; line-height: 1.1; }
.calendar-cell-tappable { cursor: pointer; }
.calendar-cell-tappable:hover { background: var(--panel-alt); }
.calendar-cell-target { background: rgba(91,140,255,0.18); }
.calendar-cell-already_booked { background: rgba(62,207,142,0.14); }
.calendar-cell-bot_confirmed { background: rgba(62,207,142,0.10); }
.calendar-cell-cancelled_after_confirm { background: rgba(154,161,192,0.10); }
.calendar-cell-deadline_expired { background: rgba(242,181,68,0.10); }
.calendar-cell-past { color: var(--text-dim); opacity: 0.55; }

.calendar-legend { display: flex; flex-wrap: wrap; gap: 10px 16px; margin-top: 12px; font-size: 12px; color: var(--text-dim); }
.calendar-legend-item { display: inline-flex; align-items: center; gap: 4px; white-space: nowrap; }
.calendar-legend-glyph { font-size: 13px; }
.calendar-discovery-banner { min-height: 16px; margin: 6px 0; font-size: 12px; }

@media (max-width: 700px) {
  .modal-calendar { width: 100%; }
  /* Large tappable day cells on phones (section 20) -- never smaller than
     a comfortable 44px touch target, and never dependent on hover. */
  .calendar-cell { height: 44px; padding: 4px 1px; }
  .calendar-cell-day { font-size: 11px; }
  .calendar-cell-glyph { font-size: 16px; }
  .calendar-table th { font-size: 10px; }
  .calendar-legend { gap: 6px 10px; font-size: 11px; }
}
