:root {
  --bg: #f4f5fb;
  --card: #ffffff;
  --ink: #1e2333;
  --muted: #6b7185;
  --line: #e3e5ef;
  --brand: #4f46e5;
  --brand-d: #4338ca;
  --green: #16a34a;
  --green-bg: #e7f7ec;
  --amber: #b45309;
  --amber-bg: #fdf1dd;
  --blue-bg: #e6ecfb;
  --shadow: 0 1px 2px rgba(20,25,50,.06), 0 6px 20px rgba(20,25,50,.05);
}

* { box-sizing: border-box; }
/* `hidden` attribute must always win, even over .gate/.app display rules. */
[hidden] { display: none !important; }
html, body { margin: 0; }
body {
  font-family: -apple-system, "Segoe UI", Roboto, Helvetica, Arial, sans-serif;
  background: var(--bg); color: var(--ink);
  font-size: 14px; line-height: 1.5;
}
h1, h3, h4 { margin: 0; }
button { font: inherit; cursor: pointer; }
a { color: var(--brand); }

/* ---- Gate ---- */
.gate {
  position: fixed; inset: 0; z-index: 100;
  display: grid; place-items: center;
  background: linear-gradient(135deg, #4f46e5, #7c3aed);
}
.gate-box {
  background: var(--card); padding: 32px 28px; border-radius: 16px;
  width: min(360px, 90vw); box-shadow: 0 20px 60px rgba(0,0,0,.3);
  display: flex; flex-direction: column; gap: 12px; text-align: center;
}
.gate-logo {
  width: 56px; height: 56px; margin: 0 auto 4px; border-radius: 14px;
  background: var(--brand); color: #fff; font-weight: 800; font-size: 22px;
  display: grid; place-items: center;
}
.gate-box h1 { font-size: 20px; }
.gate-sub { color: var(--muted); margin: 0 0 4px; }
.gate-box input {
  padding: 12px 14px; border: 1px solid var(--line); border-radius: 10px; font-size: 15px;
}
.gate-box button {
  padding: 12px; border: 0; border-radius: 10px; background: var(--brand); color: #fff;
  font-weight: 600; font-size: 15px;
}
.gate-box button:hover { background: var(--brand-d); }
.gate-err { color: #dc2626; font-size: 13px; }

/* ---- Topbar ---- */
.topbar {
  position: sticky; top: 0; z-index: 10; background: var(--card);
  border-bottom: 1px solid var(--line);
  display: flex; align-items: center; gap: 24px; padding: 0 20px; height: 56px;
}
.brand { font-weight: 700; font-size: 16px; display: flex; align-items: center; gap: 8px; }
.brand em { color: var(--muted); font-style: normal; font-weight: 500; }
.brand-logo {
  width: 28px; height: 28px; border-radius: 8px; background: var(--brand); color: #fff;
  font-size: 13px; font-weight: 800; display: grid; place-items: center;
}
.tabs { display: flex; gap: 4px; }
.tab {
  border: 0; background: transparent; color: var(--muted); padding: 8px 14px;
  border-radius: 8px; font-weight: 600;
}
.tab:hover { background: var(--bg); color: var(--ink); }
.tab.active { background: var(--brand); color: #fff; }

/* ---- Panels ---- */
main { max-width: 1200px; margin: 0 auto; padding: 20px; }
.panel { display: none; }
.panel.active { display: block; }
.panel-bar {
  display: flex; align-items: center; gap: 12px; flex-wrap: wrap; margin-bottom: 14px;
}
.panel-bar label { font-weight: 600; color: var(--muted); }
.panel-bar input[type="date"], .panel-bar input[type="search"] {
  padding: 8px 10px; border: 1px solid var(--line); border-radius: 8px; font-size: 14px; background: #fff;
}
.panel-bar input[type="search"] { min-width: min(420px, 90vw); }
.ghost {
  border: 1px solid var(--line); background: #fff; color: var(--ink);
  padding: 8px 12px; border-radius: 8px; font-weight: 600;
}
.ghost:hover { border-color: var(--brand); color: var(--brand); }
.panel-hint { color: var(--muted); margin: 0 0 12px; }
.loading { color: var(--muted); padding: 24px 0; }

/* ---- Day grid ---- */
.day-grid { display: grid; grid-template-columns: repeat(3, 1fr); gap: 16px; }
@media (max-width: 900px) { .day-grid { grid-template-columns: 1fr; } }
.person-card { background: transparent; display: flex; flex-direction: column; gap: 10px; }
.person-head {
  display: flex; align-items: center; justify-content: space-between;
  padding: 4px 2px; border-bottom: 2px solid var(--brand);
}
.person-head h3 { font-size: 16px; }
.person-prog {
  background: var(--brand); color: #fff; font-size: 12px; font-weight: 700;
  padding: 2px 8px; border-radius: 999px;
}

/* ---- Slot ---- */
.slot {
  background: var(--card); border: 1px solid var(--line); border-radius: 12px;
  padding: 12px; display: flex; flex-direction: column; gap: 8px; box-shadow: var(--shadow);
  border-left: 4px solid var(--line);
}
.slot.draft { border-left-color: var(--amber); }
.slot.done { border-left-color: var(--green); }
.slot.done.replied { border-left-color: #7c3aed; }
.slot-head { display: flex; align-items: center; justify-content: space-between; }
.slot-no {
  width: 22px; height: 22px; border-radius: 6px; background: var(--bg); color: var(--muted);
  font-weight: 700; font-size: 12px; display: grid; place-items: center;
}
.slot input[type="text"], .slot input:not([type]), .slot .f-name, .slot .f-link, .slot textarea {
  width: 100%; padding: 7px 9px; border: 1px solid var(--line); border-radius: 8px;
  font-size: 13px; font-family: inherit; resize: vertical; background: #fff;
}
.slot textarea { line-height: 1.4; }
.chk-row { display: flex; align-items: center; gap: 7px; font-size: 13px; color: var(--ink); }
.chk-row label { cursor: pointer; }
.reply-box { display: none; margin-top: 2px; }
.slot:has(.reply-toggle:checked) .reply-box { display: block; }
.slot-foot { display: flex; align-items: center; justify-content: flex-end; gap: 10px; }
.slot button[type="submit"] {
  border: 0; background: var(--brand); color: #fff;
  padding: 7px 16px; border-radius: 8px; font-weight: 600; font-size: 13px;
}
.slot button[type="submit"]:hover { background: var(--brand-d); }
.save-ok { color: var(--green); font-weight: 700; font-size: 12px; animation: saveok 2s ease forwards; }
@keyframes saveok {
  0% { opacity: 0; transform: translateY(3px); }
  15% { opacity: 1; transform: none; }
  75% { opacity: 1; }
  100% { opacity: 0; }
}
.slot.flash { animation: flashpulse 1.4s ease; }
@keyframes flashpulse {
  0% { background: var(--green-bg); box-shadow: 0 0 0 2px rgba(22,163,74,.45); }
  100% { background: var(--card); box-shadow: var(--shadow); }
}

/* ---- Badges ---- */
.badge { font-size: 11px; font-weight: 700; padding: 2px 8px; border-radius: 999px; }
.b-empty { background: var(--bg); color: var(--muted); }
.b-draft { background: var(--amber-bg); color: var(--amber); }
.b-sent { background: var(--green-bg); color: var(--green); }
.b-replied { background: #f0e9ff; color: #7c3aed; }

/* ---- Search ---- */
.hint { color: var(--muted); }
.search-empty, .search-hit {
  padding: 12px 14px; border-radius: 10px; margin-bottom: 12px;
}
.search-empty { background: var(--green-bg); color: #14532d; }
.search-hit { background: var(--amber-bg); color: #7c2d12; }
.search-table { width: 100%; border-collapse: collapse; background: var(--card); border-radius: 12px; overflow: hidden; box-shadow: var(--shadow); }
.search-table th, .search-table td { text-align: left; padding: 9px 12px; border-bottom: 1px solid var(--line); font-size: 13px; vertical-align: top; }
.search-table th { background: var(--bg); color: var(--muted); font-size: 12px; }
.cap { text-transform: capitalize; }
.msg-cell { color: var(--muted); max-width: 320px; }

/* ---- Templates ---- */
#template-list { display: flex; flex-direction: column; gap: 14px; }
.tpl { background: var(--card); border: 1px solid var(--line); border-radius: 12px; padding: 14px 16px; box-shadow: var(--shadow); }
.tpl-head { display: flex; align-items: flex-start; justify-content: space-between; gap: 12px; margin-bottom: 8px; }
.tpl-head h3 { font-size: 15px; }
.tpl-tag { color: var(--muted); font-size: 12px; }
.tpl-body {
  white-space: pre-wrap; word-break: break-word; font-family: inherit; font-size: 13.5px;
  background: var(--bg); border-radius: 8px; padding: 12px; margin: 0; line-height: 1.55;
}
.copy-btn {
  border: 1px solid var(--brand); background: #fff; color: var(--brand);
  padding: 7px 14px; border-radius: 8px; font-weight: 600; white-space: nowrap;
}
.copy-btn:hover { background: var(--brand); color: #fff; }
.copy-btn.copied { background: var(--green); border-color: var(--green); color: #fff; }

/* ---- Stats ---- */
.stats-range { color: var(--muted); margin-bottom: 12px; }
.stat-cards { display: grid; grid-template-columns: repeat(4, 1fr); gap: 14px; margin-bottom: 20px; }
@media (max-width: 700px) { .stat-cards { grid-template-columns: repeat(2, 1fr); } }
.stat-card { background: var(--card); border: 1px solid var(--line); border-radius: 12px; padding: 18px; text-align: center; box-shadow: var(--shadow); }
.stat-num { font-size: 30px; font-weight: 800; color: var(--brand); }
.stat-lbl { color: var(--muted); font-size: 12px; margin-top: 2px; }
.stats-h { margin: 8px 0 10px; }
.stats-table { width: 100%; border-collapse: collapse; background: var(--card); border-radius: 12px; overflow: hidden; box-shadow: var(--shadow); }
.stats-table th, .stats-table td { text-align: left; padding: 10px 14px; border-bottom: 1px solid var(--line); }
.stats-table th { background: var(--bg); color: var(--muted); font-size: 12px; }

.err { color: #dc2626; font-size: 13px; padding: 8px; }

/* ---- Table scroll container (wide tables never break the page) ---- */
.tbl-wrap { width: 100%; overflow-x: auto; -webkit-overflow-scrolling: touch; }
/* Only the 6-column search table is genuinely too wide for a phone; the stats
   table has 4 short columns and fits, so leave it at natural width. */
.tbl-wrap .search-table { min-width: 560px; }

/* ---- Mobile ---- */
@media (max-width: 640px) {
  body { font-size: 15px; }

  /* Topbar: stack brand over a scrollable tab row instead of clipping */
  .topbar {
    flex-direction: column; height: auto; gap: 8px;
    padding: 10px 12px; align-items: stretch;
  }
  .brand { justify-content: center; font-size: 15px; }
  .tabs {
    gap: 6px; overflow-x: auto; -webkit-overflow-scrolling: touch;
    padding-bottom: 2px;
  }
  .tab {
    flex: 1 0 auto; text-align: center; white-space: nowrap;
    padding: 9px 10px; font-size: 13px;
  }

  main { padding: 14px 12px; }
  .panel-bar { gap: 8px; }

  /* iOS zooms in on focus when input font-size < 16px — force 16px on phones */
  .slot input[type="text"], .slot input:not([type]),
  .slot .f-name, .slot .f-link, .slot textarea,
  .panel-bar input[type="date"], .panel-bar input[type="search"],
  .gate-box input {
    font-size: 16px;
  }

  /* Day cards a touch tighter */
  .slot { padding: 11px; }

  /* Templates: let the copy button drop under the title cleanly */
  .tpl-head { flex-wrap: wrap; }
  .copy-btn { flex: 0 0 auto; }
}
