/* Family Calendar – mobile-first styles. Light and dark themes follow the phone's setting. */
[hidden] { display: none !important; }
:root {
  --bg: #f5f6f8; --card: #ffffff; --text: #111827; --muted: #6b7280; --line: #e5e7eb;
  --accent: #2563eb; --accent-text: #ffffff; --bh: #dc2626; --soft: #eef2ff; --danger: #dc2626;
  --tab-h: 62px; --radius: 14px;
  --safe-top: env(safe-area-inset-top, 0px); --safe-bottom: env(safe-area-inset-bottom, 0px);
  --safe-left: env(safe-area-inset-left, 0px); --safe-right: env(safe-area-inset-right, 0px);
  color-scheme: light dark;
}
@media (prefers-color-scheme: dark) {
  :root {
    --bg: #0b0f17; --card: #151b26; --text: #e5e7eb; --muted: #9ca3af; --line: #273043;
    --accent: #3b82f6; --bh: #f87171; --soft: #1e2638; --danger: #f87171;
  }
}
* { box-sizing: border-box; -webkit-tap-highlight-color: transparent; }
html, body { margin: 0; background: var(--bg); color: var(--text); }
body {
  font: 16px/1.4 -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, Helvetica, Arial, sans-serif;
  padding-left: var(--safe-left); padding-right: var(--safe-right);
  min-height: 100vh; overscroll-behavior-y: none;
}
body.locked { overflow: hidden; }
button, input, select, textarea { font: inherit; color: inherit; }
button { cursor: pointer; }
svg { width: 24px; height: 24px; fill: none; stroke: currentColor; stroke-width: 2; stroke-linecap: round; stroke-linejoin: round; }

/* Top bar */
.topbar {
  position: sticky; top: 0; z-index: 20; background: var(--card); border-bottom: 1px solid var(--line);
  padding: calc(var(--safe-top) + 6px) 10px 6px;
}
.bar { display: flex; align-items: center; gap: 6px; min-height: 44px; }
.bar h1 { flex: 1; margin: 0; font-size: 20px; text-align: center; white-space: nowrap; }
.bar h1.left { text-align: left; padding-left: 6px; }
.icon-btn {
  min-width: 44px; min-height: 44px; border: 0; border-radius: 12px; background: transparent;
  font-size: 26px; line-height: 1; display: inline-flex; align-items: center; justify-content: center;
}
.icon-btn:active { background: var(--soft); }
.pill {
  min-height: 44px; padding: 0 14px; border-radius: 22px; border: 1px solid var(--line);
  background: var(--card); font-size: 15px; font-weight: 600; white-space: nowrap;
}
.pill.on, .pill.primary { background: var(--accent); border-color: var(--accent); color: var(--accent-text); }
.pill.danger { color: var(--danger); border-color: var(--danger); }
.pill.small { min-height: 36px; padding: 0 12px; font-size: 14px; }
.filters { display: flex; gap: 6px; overflow-x: auto; padding: 6px 0 2px; scrollbar-width: none; }
.filters::-webkit-scrollbar { display: none; }
.chip {
  flex: none; min-height: 40px; padding: 0 12px; border-radius: 20px; border: 1px solid var(--line);
  background: var(--bg); display: inline-flex; align-items: center; gap: 6px; font-size: 14px; font-weight: 600;
}
.chip.on { background: var(--text); color: var(--card); border-color: var(--text); }
.dot { display: inline-block; width: 10px; height: 10px; border-radius: 50%; flex: none; }

/* Main area */
.view { padding: 8px 8px calc(var(--tab-h) + var(--safe-bottom) + 90px); max-width: 640px; margin: 0 auto; }
body.painting .view { padding-bottom: calc(var(--tab-h) + var(--safe-bottom) + 170px); }

/* Month grid */
.weekdays, .grid { display: grid; grid-template-columns: repeat(7, 1fr); gap: 3px; }
.weekdays span { text-align: center; font-size: 12px; font-weight: 700; color: var(--muted); padding: 4px 0; }
.weekdays span:nth-child(n+6) { color: var(--bh); }
.cell {
  position: relative; min-height: 84px; border: 1px solid var(--line); border-radius: 10px; background: var(--card);
  padding: 3px 3px 14px; display: flex; flex-direction: column; align-items: stretch; gap: 2px; overflow: hidden; text-align: left;
}
.cell.other { opacity: 0.45; }
.cell.today { border: 2px solid var(--accent); box-shadow: 0 0 0 1px var(--accent); }
.cell.today .num { background: var(--accent); color: #fff; border-radius: 10px; padding: 0 5px; }
.cell .num { font-size: 13px; font-weight: 700; align-self: flex-start; }
.cell.bh .num { color: var(--bh); }
.cell.today.bh .num { color: #fff; background: var(--bh); }
.bh-mark { position: absolute; top: 3px; right: 3px; font-size: 9px; font-weight: 800; color: var(--bh); }
.cell.tinted { border-color: transparent; }
.cell .big-code { font-size: 15px; font-weight: 800; text-align: center; }
.tag {
  display: inline-block; font-size: 10px; font-weight: 800; line-height: 14px; padding: 0 3px; border-radius: 4px;
  white-space: nowrap; overflow: hidden; text-overflow: ellipsis; max-width: 100%;
}
.cell .tag { border-left: 3px solid transparent; }
.cell .note { font-size: 9px; line-height: 11px; overflow: hidden; white-space: nowrap; text-overflow: ellipsis; opacity: 0.85; }
.cell .dots { position: absolute; left: 3px; right: 3px; bottom: 3px; display: flex; gap: 2px; align-items: center; height: 8px; }
.cell .dots .dot { width: 7px; height: 7px; box-shadow: 0 0 0 1px rgba(255,255,255,0.7); }
.cell .dots small { font-size: 9px; font-weight: 700; }
.hint { color: var(--muted); font-size: 13px; text-align: center; margin: 10px 4px; }

/* Floating add button */
.fab {
  position: fixed; right: calc(16px + var(--safe-right)); bottom: calc(var(--tab-h) + var(--safe-bottom) + 14px);
  width: 56px; height: 56px; border-radius: 28px; border: 0; background: var(--accent); color: #fff;
  font-size: 32px; line-height: 1; box-shadow: 0 6px 18px rgba(0,0,0,0.25); z-index: 25;
}
body.painting .fab, .fab.hide { display: none; }

/* Paint palette */
.paintbar {
  position: fixed; left: 0; right: 0; bottom: calc(var(--tab-h) + var(--safe-bottom)); z-index: 26;
  background: var(--card); border-top: 1px solid var(--line); padding: 8px calc(10px + var(--safe-right)) 8px calc(10px + var(--safe-left));
  box-shadow: 0 -6px 16px rgba(0,0,0,0.08);
}
.paint-head { display: flex; align-items: center; gap: 8px; margin-bottom: 8px; }
.paint-head label { flex: 1; display: flex; align-items: center; gap: 6px; font-size: 14px; font-weight: 600; }
.paint-head .pill { flex: none; }
.paint-head select { flex: 1; min-width: 0; min-height: 44px; border-radius: 10px; border: 1px solid var(--line); background: var(--bg); padding: 0 8px; font-size: 16px; max-width: 150px; }
.palette { display: flex; gap: 6px; overflow-x: auto; padding-bottom: 2px; }
.swatch {
  flex: none; min-width: 52px; min-height: 48px; border-radius: 12px; border: 2px solid transparent;
  font-weight: 800; font-size: 15px; padding: 0 8px;
}
.swatch.sel { outline: 3px solid var(--text); outline-offset: 2px; }
.swatch.eraser { background: var(--bg); border: 2px dashed var(--muted); }

/* Tab bar */
.tabbar {
  position: fixed; left: 0; right: 0; bottom: 0; z-index: 30; display: grid; grid-template-columns: repeat(5, 1fr);
  background: var(--card); border-top: 1px solid var(--line);
  padding: 0 var(--safe-right) var(--safe-bottom) var(--safe-left); height: calc(var(--tab-h) + var(--safe-bottom));
}
.tabbar button {
  border: 0; background: transparent; color: var(--muted); display: flex; flex-direction: column;
  align-items: center; justify-content: center; gap: 2px; font-size: 11px; font-weight: 600; min-height: 44px;
}
.tabbar button.on { color: var(--accent); }

/* Cards, lists */
.card { background: var(--card); border: 1px solid var(--line); border-radius: var(--radius); padding: 12px; margin-bottom: 10px; }
.card h2 { font-size: 17px; margin: 0 0 8px; }
.card p { margin: 6px 0; }
.muted { color: var(--muted); }
.small { font-size: 13px; }
.ag-day { background: var(--card); border: 1px solid var(--line); border-radius: var(--radius); margin-bottom: 10px; overflow: hidden; }
.ag-day.today { border-color: var(--accent); }
.ag-head {
  width: 100%; min-height: 44px; border: 0; background: var(--soft); display: flex; justify-content: space-between;
  align-items: center; padding: 0 12px; font-weight: 700;
}
.ag-head span { color: var(--muted); font-weight: 600; font-size: 14px; }
.ag-list { list-style: none; margin: 0; padding: 4px 0; }
.ag-list li { padding: 0; }
.item {
  width: 100%; min-height: 44px; border: 0; background: transparent; display: flex; align-items: center; gap: 10px;
  padding: 6px 12px; text-align: left;
}
.item .bar-col { width: 4px; align-self: stretch; border-radius: 2px; flex: none; }
.item .time { font-size: 13px; color: var(--muted); min-width: 84px; flex: none; }
.item .title { flex: 1; font-weight: 600; overflow-wrap: anywhere; }
.item .who { font-size: 12px; color: var(--muted); flex: none; }
.empty { padding: 10px 12px; color: var(--muted); font-size: 14px; }
.bh-line { color: var(--bh); font-weight: 700; font-size: 14px; padding: 8px 12px 0; }

/* Week grid */
.week-grid { display: grid; grid-template-columns: 64px repeat(7, 1fr); gap: 3px; align-items: stretch; }
.wk-day {
  border: 1px solid var(--line); background: var(--card); border-radius: 8px; min-height: 48px; padding: 2px;
  display: flex; flex-direction: column; align-items: center; justify-content: center; font-size: 11px; color: var(--muted);
}
.wk-day b { font-size: 15px; color: var(--text); }
.wk-day.today { border-color: var(--accent); background: var(--soft); }
.wk-day.bh b { color: var(--bh); }
.wk-name { display: flex; align-items: center; gap: 4px; font-size: 12px; font-weight: 700; overflow: hidden; }
.wk-name span { overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.wk-cell {
  border: 1px solid var(--line); border-radius: 8px; min-height: 44px; background: var(--card);
  display: flex; align-items: center; justify-content: center; font-size: 12px; font-weight: 800; padding: 1px; overflow: hidden;
}

/* Summary */
table.sum { width: 100%; border-collapse: collapse; }
table.sum th, table.sum td { padding: 8px 6px; border-bottom: 1px solid var(--line); text-align: left; }
table.sum td.n, table.sum th.n { text-align: right; font-variant-numeric: tabular-nums; }
table.sum tfoot td { font-weight: 800; border-bottom: 0; }

/* Forms */
.field { display: flex; flex-direction: column; gap: 4px; margin-bottom: 12px; }
.field > span { font-size: 13px; font-weight: 700; color: var(--muted); }
input[type=text], input[type=date], input[type=time], input[type=number], select, textarea {
  min-height: 44px; border: 1px solid var(--line); border-radius: 10px; background: var(--bg); padding: 8px 10px;
  font-size: 16px; width: 100%;
}
textarea { resize: vertical; }
input[type=color] { width: 44px; height: 44px; border: 1px solid var(--line); border-radius: 10px; padding: 2px; background: var(--bg); flex: none; }
.check { display: flex; align-items: center; gap: 10px; min-height: 44px; font-weight: 600; }
.check input { width: 22px; height: 22px; }
.two { display: grid; grid-template-columns: 1fr 1fr; gap: 10px; }
.row { display: flex; align-items: center; gap: 8px; min-height: 48px; }
.row .grow { flex: 1; min-width: 0; }
.row .who { flex: 1; display: flex; align-items: center; gap: 8px; font-weight: 600; }
.row select { width: 170px; flex: none; }
.btns { display: flex; flex-wrap: wrap; gap: 8px; margin-top: 8px; }
.edit-row { border-bottom: 1px solid var(--line); padding: 8px 0; }
.edit-row:last-child { border-bottom: 0; }
.edit-row .row input[type=text] { min-width: 0; }
.code-in { width: 70px !important; flex: none; }
.times { display: flex; gap: 6px; align-items: center; margin-top: 6px; }
.times input { flex: 1; min-width: 0; }
.times span { font-size: 13px; color: var(--muted); }
ol.steps { padding-left: 20px; margin: 6px 0; }
ol.steps li { margin: 4px 0; }

/* Bottom sheet */
.backdrop { position: fixed; inset: 0; background: rgba(0,0,0,0.4); z-index: 40; }
.sheet {
  position: fixed; left: 0; right: 0; bottom: 0; z-index: 41; max-height: 88vh; overflow-y: auto;
  background: var(--card); border-radius: 18px 18px 0 0; max-width: 640px; margin: 0 auto;
  padding: 6px calc(16px + var(--safe-right)) calc(20px + var(--safe-bottom)) calc(16px + var(--safe-left));
  transform: translateY(100%); transition: transform 0.22s ease-out; -webkit-overflow-scrolling: touch;
}
.sheet.open { transform: translateY(0); }
.sheet-head { display: flex; align-items: center; gap: 8px; position: sticky; top: -6px; background: var(--card); padding: 6px 0; z-index: 1; }
.sheet-head h2 { flex: 1; margin: 0; font-size: 18px; }
.sheet section { margin: 10px 0 16px; }
.sheet h3 { font-size: 14px; text-transform: uppercase; letter-spacing: 0.04em; color: var(--muted); margin: 0 0 6px; }
.sec-head { display: flex; justify-content: space-between; align-items: center; margin-bottom: 6px; }
.sec-head h3 { margin: 0; }
.badge { display: inline-block; padding: 4px 10px; border-radius: 12px; font-size: 14px; font-weight: 700; margin: 4px 0; }
.badge.bh { background: rgba(220,38,38,0.12); color: var(--bh); }
.ev-list { list-style: none; margin: 0; padding: 0; border: 1px solid var(--line); border-radius: 12px; overflow: hidden; }
.ev-list li + li { border-top: 1px solid var(--line); }

.toast {
  position: fixed; left: 50%; bottom: calc(var(--tab-h) + var(--safe-bottom) + 84px); transform: translateX(-50%) translateY(20px);
  background: var(--text); color: var(--card); padding: 10px 16px; border-radius: 20px; font-size: 14px; font-weight: 600;
  opacity: 0; pointer-events: none; transition: opacity 0.2s, transform 0.2s; z-index: 60; max-width: 90vw; text-align: center;
}
.toast.show { opacity: 1; transform: translateX(-50%) translateY(0); }
@media (min-width: 500px) { .cell { min-height: 90px; } .cell .tag { font-size: 11px; } }
/* Family sync */
.sync-sub { font-size: 15px; margin: 14px 0 4px; }
.sync-code { font-family: ui-monospace, SFMono-Regular, Menlo, monospace; font-size: 13px; background: var(--soft); border: 1px solid var(--line); border-radius: 10px; padding: 8px 10px; word-break: break-all; user-select: all; -webkit-user-select: all; }
.sync-status { padding: 8px 10px; border-radius: 10px; background: var(--soft); }
.sync-status[data-state="ok"] { border-left: 4px solid #16a34a; }
.sync-status[data-state="syncing"] { border-left: 4px solid var(--accent); }
.sync-status[data-state="offline"] { border-left: 4px solid #ca8a04; }
.sync-status[data-state="error"] { border-left: 4px solid var(--danger); }
#joinCode { min-height: 44px; }
