:root {
  --bg: #f6f6f4;
  --panel: #ffffff;
  --panel-2: #fbfbfa;
  --ink: #1c1c1a;
  --ink-2: #5f5e5a;
  --ink-3: #90908a;
  --line: #e5e4e0;
  --line-2: #d3d1c7;
  --accent: #185fa5;
  --accent-bg: #e6f1fb;
  --ok: #0f6e56;
  --ok-bg: #e1f5ee;
  --warn: #854f0b;
  --warn-bg: #faeeda;
  --bad: #a32d2d;
  --bad-bg: #fcebeb;
  --radius: 8px;
  --shadow: 0 1px 2px rgba(0,0,0,.04), 0 8px 24px rgba(0,0,0,.06);
  --pad: 20px;          /* page gutter - shrinks on phones */
}

* { box-sizing: border-box; }

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

body {
  margin: 0;
  background: var(--bg);
  color: var(--ink);
  font: 400 14px/1.55 -apple-system, "Segoe UI", Roboto, Helvetica, Arial, sans-serif;
  -webkit-font-smoothing: antialiased;
  /* Safety net so nothing can push the page sideways. `clip` rather than
     `hidden` on purpose: `hidden` makes body a scroll container, which breaks
     the sticky header. */
  overflow-x: clip;
}

button, input, select, textarea { font: inherit; color: inherit; }
a { color: var(--accent); }

.hidden { display: none !important; }
.muted { color: var(--ink-2); }
.dim { color: var(--ink-3); }
.small { font-size: 12px; }
.row { display: flex; align-items: center; gap: 10px; }
.spread { display: flex; align-items: center; justify-content: space-between; gap: 12px; }
.grow { flex: 1; min-width: 0; }
.wrap { flex-wrap: wrap; }
.block { width: 100%; }

/* Swapped by pointer type: drag-and-drop copy vs. tap copy. */
.touch-only { display: none; }
@media (hover: none) and (pointer: coarse) {
  .touch-only { display: block; }
  .mouse-only { display: none; }
}

/* ---------- login ---------- */
#login {
  min-height: 100vh;
  min-height: 100svh;
  display: flex;
  align-items: center;
  justify-content: center;
  padding: 24px 16px;
}
.login-card {
  width: 100%;
  max-width: 360px;
  background: var(--panel);
  border: 1px solid var(--line);
  border-radius: 14px;
  padding: 28px;
  box-shadow: var(--shadow);
}
.brand { font-size: 20px; font-weight: 600; letter-spacing: -.01em; white-space: nowrap; }
.brand span { color: var(--ink-3); font-weight: 400; }

/* ---------- shell ---------- */
header.topbar {
  position: sticky;
  top: 0;
  z-index: 30;
  background: rgba(246,246,244,.92);
  backdrop-filter: blur(8px);
  border-bottom: 1px solid var(--line);
  padding: 10px var(--pad);
}
.topbar .bar {
  display: flex; align-items: center; justify-content: space-between; gap: 12px;
  max-width: 1240px; margin: 0 auto;
}
.topbar .bar-main { display: flex; align-items: center; gap: 10px; min-width: 0; }
.topbar .bar-side { display: flex; align-items: center; gap: 10px; min-width: 0; }
.cycle-select { width: auto; min-width: 150px; }

.avatar {
  display: none;
  flex: 0 0 auto;
  width: 38px; height: 38px; border-radius: 999px;
  background: var(--ink); color: #fff; border: 0;
  font-size: 13px; font-weight: 600; letter-spacing: .02em;
  cursor: pointer;
}

nav.tabs {
  display: flex; gap: 4px; margin: 10px auto 0; max-width: 1240px; flex-wrap: wrap;
}
nav.tabs button {
  background: none; border: 0; padding: 6px 12px; border-radius: 999px;
  color: var(--ink-2); cursor: pointer; white-space: nowrap;
}
nav.tabs button:hover { background: #ecebe7; }
nav.tabs button.on { background: var(--ink); color: #fff; }

main {
  padding: var(--pad);
  max-width: 1240px;
  margin: 0 auto;
  margin-bottom: calc(60px + env(safe-area-inset-bottom));
}

/* ---------- controls ---------- */
.btn {
  background: var(--panel); border: 1px solid var(--line-2); border-radius: var(--radius);
  padding: 7px 13px; cursor: pointer; transition: background .12s, border-color .12s;
}
.btn:hover { background: var(--panel-2); border-color: var(--ink-3); }
.btn:active { transform: scale(.985); }
.btn.primary { background: var(--ink); border-color: var(--ink); color: #fff; }
.btn.primary:hover { background: #35342f; }
.btn.go { background: var(--ok); border-color: var(--ok); color: #fff; }
.btn.go:hover { background: #0c5a46; }
.btn.danger { color: var(--bad); border-color: #eccaca; }
.btn.danger:hover { background: var(--bad-bg); }
.btn.sm { padding: 4px 9px; font-size: 12px; border-radius: 6px; }
.btn:disabled { opacity: .45; cursor: not-allowed; }

input[type=text], input[type=password], input[type=date], input[type=number], input[type=url],
select, textarea {
  width: 100%; background: var(--panel); border: 1px solid var(--line-2);
  border-radius: var(--radius); padding: 7px 10px; outline: none;
}
input:focus, select:focus, textarea:focus { border-color: var(--accent); box-shadow: 0 0 0 3px var(--accent-bg); }
textarea { resize: vertical; min-height: 64px; }
label.field { display: block; margin-bottom: 12px; }
label.field > span { display: block; font-size: 12px; color: var(--ink-2); margin-bottom: 4px; }

.filter-select { width: auto; min-width: 180px; }

/* ---------- cards ---------- */
.card {
  background: var(--panel); border: 1px solid var(--line); border-radius: 12px;
  padding: 16px 18px; margin-bottom: 14px;
}
.metrics { display: grid; grid-template-columns: repeat(auto-fit, minmax(140px, 1fr)); gap: 12px; margin-bottom: 18px; }
.metric { background: var(--panel); border: 1px solid var(--line); border-radius: 12px; padding: 14px 16px; }
.metric .k { font-size: 12px; color: var(--ink-2); }
.metric .v { font-size: 26px; font-weight: 600; letter-spacing: -.02em; margin-top: 2px; }

.pill { font-size: 12px; padding: 3px 10px; border-radius: 999px; white-space: nowrap; }
.pill.ok { background: var(--ok-bg); color: var(--ok); }
.pill.warn { background: var(--warn-bg); color: var(--warn); }
.pill.bad { background: var(--bad-bg); color: var(--bad); }
.pill.info { background: var(--accent-bg); color: var(--accent); }
.pill.neutral { background: #eeedea; color: var(--ink-2); }

/* ---------- client progress ---------- */
/* The stage count is dynamic, so the views set --cols inline. Keeping it in a
   custom property (rather than grid-template-columns) is what lets the media
   queries below override the layout at all. */
.stages { display: grid; grid-template-columns: repeat(var(--cols, 6), minmax(0,1fr)); gap: 10px; }
.stage-col .lbl { font-size: 12px; color: var(--ink-2); }
.stage-col .num { font-size: 20px; font-weight: 600; letter-spacing: -.02em; }
.stage-col .num small { font-size: 12px; font-weight: 400; color: var(--ink-3); }
.bar { height: 4px; background: #eae9e5; border-radius: 2px; margin-top: 6px; overflow: hidden; }
.bar > i { display: block; height: 100%; background: var(--ok); border-radius: 2px; }
.bar > i.late { background: var(--warn); }
.bar > i.post { background: var(--accent); }

/* ---------- board ---------- */
.board { display: grid; grid-template-columns: repeat(var(--cols, 6), minmax(190px,1fr)); gap: 12px; align-items: start; }
.col { background: #efeeea; border-radius: 12px; padding: 10px; min-width: 0; }
.col h4 { margin: 0 0 8px; font-size: 12px; font-weight: 600; color: var(--ink-2); text-transform: none; }
.vcard {
  background: var(--panel); border: 1px solid var(--line); border-radius: 10px;
  padding: 10px 11px; margin-bottom: 8px; cursor: pointer;
}
.vcard:hover { border-color: var(--ink-3); }
.vcard.blocked { border-color: #eccaca; background: #fffafa; }
.vcard .t { font-weight: 500; margin-bottom: 3px; }
.vcard .m { font-size: 12px; color: var(--ink-3); }

/* ---------- table ---------- */
table.grid { width: 100%; border-collapse: collapse; background: var(--panel); border-radius: 12px; overflow: hidden; }
table.grid th {
  text-align: left; font-size: 12px; font-weight: 500; color: var(--ink-2);
  padding: 10px 12px; border-bottom: 1px solid var(--line); background: var(--panel-2);
}
table.grid td { padding: 9px 12px; border-bottom: 1px solid var(--line); vertical-align: middle; }
table.grid tr:last-child td { border-bottom: 0; }
table.grid tr.clickable:hover td { background: var(--panel-2); cursor: pointer; }

/* ---------- modal ---------- */
.backdrop {
  position: fixed; inset: 0; background: rgba(28,28,26,.4); z-index: 60;
  display: flex; align-items: flex-start; justify-content: center; padding: 40px 20px; overflow: auto;
}
.modal {
  width: 100%; max-width: 620px; background: var(--panel); border-radius: 14px;
  box-shadow: var(--shadow); padding: 22px 24px;
}
.modal h3 { margin: 0 0 2px; font-size: 17px; font-weight: 600; }
.modal .sub { color: var(--ink-2); font-size: 13px; margin-bottom: 16px; }
.hr { height: 1px; background: var(--line); margin: 16px 0; }
.hist { max-height: 190px; overflow: auto; font-size: 13px; }
.hist div { padding: 5px 0; border-bottom: 1px solid var(--line); }
.hist div:last-child { border-bottom: 0; }
.stack { display: flex; flex-direction: column; align-items: stretch; gap: 8px; }

.toast {
  position: fixed; bottom: calc(22px + env(safe-area-inset-bottom)); left: 50%;
  transform: translateX(-50%);
  background: var(--ink); color: #fff; padding: 10px 18px; border-radius: 999px;
  z-index: 90; box-shadow: var(--shadow); font-size: 13px;
  max-width: calc(100vw - 32px); text-align: center;
}
.toast.err { background: var(--bad); }

.empty { text-align: center; padding: 48px 20px; color: var(--ink-2); }

.act-when { display: inline-block; min-width: 130px; }

/* ---------- drag and drop ---------- */
.vcard { user-select: none; -webkit-user-select: none; -webkit-touch-callout: none; }
.vcard:active { cursor: grabbing; }
.vcard.dragging { opacity: .4; }
.col { transition: background .12s, box-shadow .12s; }
.col .dropzone { min-height: 44px; }
.col.over { background: var(--ok-bg); box-shadow: inset 0 0 0 2px #9fe1cb; }
.col.no { background: var(--bad-bg); box-shadow: inset 0 0 0 2px #f7c1c1; cursor: not-allowed; }
.col h4 .dim { font-weight: 400; }

/* HTML5 drag never fires on touch, so don't let iOS try to lift the card. */
@media (hover: none) and (pointer: coarse) {
  .vcard { -webkit-user-drag: none; }
}

/* ---------- plan a month ---------- */
.planlist { max-height: 340px; overflow: auto; border: 1px solid var(--line); border-radius: var(--radius); }
.planrow {
  display: flex; align-items: center; gap: 10px; padding: 9px 12px;
  border-bottom: 1px solid var(--line); cursor: pointer;
}
.planrow:last-child { border-bottom: 0; }
.planrow:hover { background: var(--panel-2); }
.planrow.on { background: var(--ok-bg); }
.planrow input[type=checkbox] { width: auto; margin: 0; }
.planrow input[type=number] { padding: 4px 8px; }

/* ================================================================== *
 * Tablet
 * ================================================================== */
@media (max-width: 900px) {
  /* auto-fit rather than a fixed count, so this holds if a stage is ever added. */
  .stages { grid-template-columns: repeat(auto-fit, minmax(120px, 1fr)); }
  /* Drop the 190px floor so the columns shrink instead of overflowing the page. */
  .board { grid-template-columns: repeat(var(--cols, 4), minmax(0, 1fr)); }
}

/* ================================================================== *
 * Large phone / small tablet
 * ================================================================== */
@media (max-width: 760px) {
  /* iOS zooms the page whenever a focused field is under 16px. */
  input[type=text], input[type=password], input[type=date], input[type=number],
  input[type=url], select, textarea { font-size: 16px; padding: 9px 11px; }

  /* Tables turn into stacked cards - one block per row, labels inline. */
  table.grid { display: block; border: 1px solid var(--line); }
  table.grid thead { display: none; }
  table.grid tbody, table.grid tr { display: block; width: 100%; }
  table.grid tr { padding: 10px 12px; border-bottom: 1px solid var(--line); }
  table.grid tr:last-child { border-bottom: 0; }
  table.grid td {
    display: flex; align-items: center; justify-content: space-between; gap: 12px;
    padding: 3px 0; border-bottom: 0; text-align: left !important;
  }
  table.grid td::before {
    content: attr(data-label);
    flex: 0 0 auto; font-size: 12px; color: var(--ink-2);
  }
  table.grid td:not([data-label])::before { content: none; }
  table.grid td:empty { display: none; }
  table.grid td.strong { font-weight: 500; }
  table.grid td.act { justify-content: flex-end; padding-top: 8px; }
  .hide-sm { display: none !important; }
}

/* ================================================================== *
 * Phone
 * ================================================================== */
@media (max-width: 640px) {
  :root { --pad: 14px; }

  /* One compact header row: brand, month picker, account button. */
  .topbar .bar { gap: 8px; }
  .topbar .brand { font-size: 16px; }
  .topbar .brand span { display: none; }
  .topbar .bar-main { flex: 1; }
  .cycle-select { flex: 1; width: 100%; min-width: 0; }
  #whoami, #pwBtn, #logoutBtn { display: none; }
  .avatar { display: block; }

  /* Tabs scroll sideways rather than wrapping onto a second sticky row. */
  nav.tabs {
    flex-wrap: nowrap;
    overflow-x: auto;
    margin-inline: calc(var(--pad) * -1);
    padding: 0 var(--pad);
    -webkit-overflow-scrolling: touch;
    scrollbar-width: none;
  }
  nav.tabs::-webkit-scrollbar { display: none; }
  nav.tabs button { flex: 0 0 auto; }

  .spread { flex-wrap: wrap; }
  .card { padding: 14px; }

  /* Toolbars go full width once the row has wrapped. */
  .row.actions { width: 100%; }
  .row.actions > .btn { flex: 1; }
  .toolbar > .filter-select, .toolbar > .btn { flex: 1 1 100%; width: 100%; min-width: 0; }

  .metrics { grid-template-columns: repeat(2, minmax(0,1fr)); gap: 10px; }
  .metric { padding: 12px 13px; }
  .metric .v { font-size: 22px; }
  .metric:last-child:nth-child(odd) { grid-column: span 2; }

  /* Four columns are unreadable at this width - make the board a swipeable
     row of stages instead, one nearly-full-width column at a time. */
  .board {
    display: flex;
    grid-template-columns: none;
    overflow-x: auto;
    scroll-snap-type: x mandatory;
    scroll-padding-left: var(--pad);
    gap: 10px;
    margin-inline: calc(var(--pad) * -1);
    padding: 0 var(--pad) 6px;
    -webkit-overflow-scrolling: touch;
    scrollbar-width: none;
  }
  .board::-webkit-scrollbar { display: none; }
  .board .col { flex: 0 0 86%; scroll-snap-align: start; }
  .board .col .dropzone { min-height: 60px; }

  .act-when { display: block; min-width: 0; }

  /* Modals sit on the bottom edge like a sheet, with a pinned action bar. */
  .backdrop { padding: 0; align-items: flex-end; overflow: hidden; }
  .modal {
    max-width: none;
    border-radius: 16px 16px 0 0;
    padding: 18px 16px 0;
    max-height: 92vh;
    max-height: 92dvh;
    overflow-y: auto;
    -webkit-overflow-scrolling: touch;
  }
  .modal > .spread:last-child {
    position: sticky;
    bottom: 0;
    flex-direction: column;
    align-items: stretch;
    gap: 8px;
    background: var(--panel);
    border-top: 1px solid var(--line);
    margin: 14px -16px 0;
    padding: 12px 16px calc(12px + env(safe-area-inset-bottom));
  }
  .modal > .spread:last-child .row { flex-wrap: wrap; }
  .modal > .spread:last-child .row > .btn { flex: 1 1 auto; }
  .modal > .spread:last-child > span:empty { display: none; }

  .planlist { max-height: 45vh; }
  .planrow input[type=number] { width: 68px; }

  .hist { max-height: 150px; }
}

/* ================================================================== *
 * Small phone
 * ================================================================== */
@media (max-width: 520px) {
  .stages { grid-template-columns: repeat(2, minmax(0,1fr)); }
}

/* ================================================================== *
 * Touch: everything tappable needs a real target
 * ================================================================== */
@media (hover: none) and (pointer: coarse) {
  .btn { min-height: 44px; padding: 10px 14px; }
  .btn.sm { min-height: 36px; padding: 7px 11px; }
  nav.tabs button { min-height: 38px; padding: 8px 14px; }
  .planrow { padding: 12px; }
  .planrow input[type=checkbox] { width: 20px; height: 20px; }
  .vcard { padding: 12px; }
  table.grid tr.clickable { cursor: default; }
  /* Hover styling sticks after a tap on touch devices - turn it off. */
  .btn:hover { background: var(--panel); border-color: var(--line-2); }
  .btn.primary:hover { background: var(--ink); }
  .btn.go:hover { background: var(--ok); }
  .btn.danger:hover { background: var(--panel); }
  .vcard:hover { border-color: var(--line); }
  nav.tabs button:hover { background: none; }
  nav.tabs button.on:hover { background: var(--ink); }
  table.grid tr.clickable:hover td { background: transparent; }
  .planrow:hover { background: transparent; }
  .planrow.on, .planrow.on:hover { background: var(--ok-bg); }
}
