:root {
  --bg: #f6f7f9;
  --surface: #ffffff;
  --surface-2: #f0f2f5;
  --text: #1c2026;
  --muted: #67707d;
  --border: #dde1e6;
  --line: #eceef1;
  --accent: #2d6cdf;
  --danger: #c93b3b;
  --ok: #2e8a57;
  --weekend: #f9fafb;
  --today: #fff8e1;
  --shadow: 0 1px 2px rgb(0 0 0 / 6%), 0 4px 16px rgb(0 0 0 / 6%);
  --p-havencraft: #2e9d6b;
  --p-other: #5868d8;
  --hour-h: 44px;
  color-scheme: light;
}

@media (prefers-color-scheme: dark) {
  :root {
    --bg: #14171b;
    --surface: #1c2026;
    --surface-2: #242a31;
    --text: #e6e9ed;
    --muted: #9aa3ae;
    --border: #333a43;
    --line: #2a3038;
    --accent: #6b9bff;
    --danger: #ff7a7a;
    --ok: #5fcf8f;
    --weekend: #191c21;
    --today: #2b2815;
    --shadow: 0 1px 2px rgb(0 0 0 / 30%), 0 4px 16px rgb(0 0 0 / 30%);
    --p-havencraft: #33a874;
    --p-other: #6a79e6;
    color-scheme: dark;
  }
}

* { box-sizing: border-box; }

html, body { margin: 0; }

body {
  background: var(--bg);
  color: var(--text);
  font: 14px/1.45 system-ui, -apple-system, "Segoe UI", Roboto, sans-serif;
  font-variant-numeric: tabular-nums;
}

button, input, select, textarea { font: inherit; color: inherit; }

button {
  border: 1px solid var(--border);
  background: var(--surface);
  border-radius: 6px;
  padding: 6px 12px;
  cursor: pointer;
}
button:hover:not(:disabled) { background: var(--surface-2); }
button:disabled { opacity: 0.5; cursor: default; }
button.primary { background: var(--accent); border-color: var(--accent); color: #fff; }
button.primary:hover:not(:disabled) { filter: brightness(1.08); background: var(--accent); }
button.danger { color: var(--danger); }
button.ghost { border-color: transparent; background: transparent; }
button.small { padding: 2px 8px; font-size: 12px; }

input, select, textarea {
  border: 1px solid var(--border);
  background: var(--surface);
  border-radius: 6px;
  padding: 6px 8px;
}

/* Top bar */
.topbar {
  display: flex;
  align-items: center;
  gap: 16px;
  padding: 10px 20px;
  background: var(--surface);
  border-bottom: 1px solid var(--border);
  position: sticky;
  top: 0;
  z-index: 20;
}
.brand { font-weight: 650; letter-spacing: 0.2px; }
.topbar nav { display: flex; gap: 4px; }
.topbar nav a {
  color: var(--muted);
  text-decoration: none;
  padding: 5px 12px;
  border-radius: 6px;
}
.topbar nav a.active { background: var(--surface-2); color: var(--text); font-weight: 600; }
.spacer { flex: 1; }
.user { color: var(--muted); }

main { padding: 16px 20px 40px; max-width: 1280px; margin: 0 auto; }

/* View header */
.view-head {
  display: flex;
  align-items: center;
  flex-wrap: wrap;
  gap: 10px 16px;
  margin-bottom: 14px;
}
.view-head h1 { font-size: 20px; margin: 0; font-weight: 650; min-width: 220px; }
.pager { display: flex; gap: 4px; }
.totals { display: flex; gap: 14px; flex-wrap: wrap; color: var(--muted); }
.totals b { color: var(--text); font-weight: 600; }
.swatch { display: inline-block; width: 10px; height: 10px; border-radius: 3px; margin-right: 5px; vertical-align: -1px; }

.card {
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: 10px;
  box-shadow: var(--shadow);
  overflow: hidden;
}

/* Month table */
table { border-collapse: collapse; width: 100%; }
.month th, .month td { padding: 7px 10px; text-align: left; border-bottom: 1px solid var(--line); white-space: nowrap; }
.month thead th { font-size: 12px; color: var(--muted); font-weight: 600; background: var(--surface-2); }
.month td.num, .month th.num { text-align: right; }
.month tbody tr.day { cursor: pointer; }
.month tbody tr.day:hover { background: var(--surface-2); }
.month tbody tr.day.weekend { background: var(--weekend); }
.month tbody tr.day.today { background: var(--today); }
.month tbody tr.day.empty td { color: var(--muted); }
.month tfoot td { font-weight: 650; background: var(--surface-2); }
.chev { width: 28px; color: var(--muted); transition: transform 0.15s; display: inline-block; }
tr.day.open .chev { transform: rotate(90deg); }
tr.detail > td { padding: 0 10px 12px 38px; background: var(--surface-2); }
tr.detail[hidden] { display: none; }

.entries { margin-top: 8px; background: var(--surface); border: 1px solid var(--border); border-radius: 8px; overflow: hidden; }
.entries th, .entries td { padding: 6px 10px; border-bottom: 1px solid var(--line); text-align: left; }
.entries thead th { font-size: 12px; color: var(--muted); font-weight: 600; }
.entries tr:last-child td { border-bottom: none; }
.entries tr.recording td { background-image: repeating-linear-gradient(135deg, color-mix(in srgb, var(--muted) 16%, transparent) 0 3px, transparent 3px 9px); }
.entries td.note { white-space: normal; max-width: 360px; color: var(--muted); }
.entries .actions { text-align: right; white-space: nowrap; }
.detail-foot { margin-top: 8px; }

.badge {
  display: inline-flex;
  align-items: center;
  gap: 4px;
  font-size: 12px;
  padding: 1px 8px;
  border-radius: 999px;
  border: 1px solid var(--border);
  color: var(--muted);
}
.badge.approved { color: var(--ok); border-color: color-mix(in srgb, var(--ok) 40%, transparent); }
.badge.recording { color: var(--danger); border-color: color-mix(in srgb, var(--danger) 40%, transparent); }
.dot { width: 7px; height: 7px; border-radius: 50%; background: currentColor; animation: pulse 1.6s infinite; }
@keyframes pulse { 50% { opacity: 0.3; } }

.status-all { color: var(--ok); }

/* Week calendar */
.week-wrap { overflow-x: auto; }
.week { min-width: 760px; }
.week-head, .week-body { display: grid; grid-template-columns: 56px repeat(7, 1fr); }
.week-head {
  position: sticky;
  top: 0;
  z-index: 5;
  background: var(--surface);
  border-bottom: 1px solid var(--border);
}
.week-head .day-head { padding: 8px 8px 6px; border-left: 1px solid var(--line); }
.week-head .day-head .dname { font-weight: 600; }
.week-head .day-head .dsum { color: var(--muted); font-size: 12px; }
.week-head .day-head.today .dname { color: var(--accent); }
.week-scroll { max-height: calc(100vh - 210px); overflow-y: auto; position: relative; }
.week-body { height: calc(24 * var(--hour-h)); position: relative; }
.hours { position: relative; }
.hours span {
  position: absolute;
  right: 8px;
  transform: translateY(-50%);
  font-size: 11px;
  color: var(--muted);
}
.week-body { user-select: none; -webkit-user-select: none; }
.day-col {
  position: relative;
  border-left: 1px solid var(--line);
  background-image: repeating-linear-gradient(to bottom, var(--line) 0 1px, transparent 1px var(--hour-h));
  cursor: crosshair;
}
/* Preview while dragging a new entry. */
.block.ghost {
  pointer-events: none;
  z-index: 5;
  color: var(--text);
  background-color: color-mix(in srgb, var(--pc) 28%, transparent);
  border: 2px dashed var(--pc);
}
.zoom button { min-width: 32px; font-weight: 600; }
.day-col.weekend { background-color: var(--weekend); }
.day-col.today { background-color: var(--today); }
.now-line { position: absolute; left: 0; right: 0; height: 2px; background: var(--danger); z-index: 3; pointer-events: none; }
.block {
  position: absolute;
  border-radius: 5px;
  padding: 3px 6px;
  font-size: 12px;
  line-height: 1.3;
  color: #fff;
  background-color: var(--pc);
  overflow: hidden;
  cursor: pointer;
  border: 1px solid rgb(0 0 0 / 12%);
  z-index: 2;
}
.block:hover { filter: brightness(1.08); z-index: 4; }
.block .bt { font-weight: 600; }
.block .bt.cycle { display: inline-block; cursor: pointer; border-radius: 4px; padding: 0 4px; margin: 0 -4px; }
.block .bt.cycle:hover { background: rgb(255 255 255 / 22%); }
.block .bs { opacity: 0.9; }
/* Resize handles: visible on hover, drag to move the start or end. */
.block .handle {
  position: absolute;
  left: 0;
  right: 0;
  height: 9px;
  cursor: ns-resize;
  opacity: 0;
  transition: opacity 0.12s;
  touch-action: none;
  z-index: 1;
}
.block .handle.top { top: 0; }
.block .handle.bottom { bottom: 0; }
.block .handle::after {
  content: '';
  position: absolute;
  left: 50%;
  top: 50%;
  width: 28px;
  height: 4px;
  margin: -2px 0 0 -14px;
  border-radius: 2px;
  background: rgb(255 255 255 / 90%);
  box-shadow: 0 0 0 1px rgb(0 0 0 / 30%);
}
.block:hover .handle, .block.resizing .handle { opacity: 1; }
.block.resizing { z-index: 6; box-shadow: 0 2px 10px rgb(0 0 0 / 25%); }
/* Still recording: transparent, hatched in the project color. */
.block.recording {
  color: var(--text);
  background-color: color-mix(in srgb, var(--pc) 12%, transparent);
  background-image: repeating-linear-gradient(135deg, color-mix(in srgb, var(--pc) 40%, transparent) 0 3px, transparent 3px 9px);
  border: 1.5px dashed var(--pc);
}
.block .lock { float: right; margin-left: 4px; }

/* Dialogs */
dialog {
  border: 1px solid var(--border);
  border-radius: 12px;
  background: var(--surface);
  color: var(--text);
  box-shadow: var(--shadow);
  padding: 0;
  width: min(440px, calc(100vw - 32px));
}
dialog::backdrop { background: rgb(0 0 0 / 35%); }
dialog form { padding: 18px 20px; display: grid; gap: 12px; }
dialog h2 { margin: 0 0 2px; font-size: 17px; }
.field { display: grid; gap: 4px; min-width: 0; }
.field > input, .field > select, .field > textarea { width: 100%; min-width: 0; }
.field label { font-size: 12px; color: var(--muted); font-weight: 600; }
.row { display: grid; grid-template-columns: 1fr 1fr 1fr; gap: 10px; }
.hint { font-size: 12px; color: var(--muted); min-height: 1em; }
.error { color: var(--danger); font-size: 13px; min-height: 1em; }
.dialog-actions { display: flex; gap: 8px; justify-content: flex-end; align-items: center; margin-top: 4px; }
.dialog-actions .left { margin-right: auto; }
.locked-note { background: var(--surface-2); border-radius: 8px; padding: 8px 10px; font-size: 13px; color: var(--muted); }

/* Toast */
#toast {
  position: fixed;
  bottom: 20px;
  left: 50%;
  transform: translateX(-50%) translateY(20px);
  background: var(--text);
  color: var(--bg);
  padding: 8px 16px;
  border-radius: 8px;
  opacity: 0;
  pointer-events: none;
  transition: opacity 0.2s, transform 0.2s;
  z-index: 50;
}
#toast.show { opacity: 1; transform: translateX(-50%) translateY(0); }

/* Login */
.login-page { min-height: 100vh; display: grid; place-items: center; padding: 16px; }
.login-card { width: min(360px, 100%); padding: 24px; }
.login-card h1 { font-size: 20px; margin: 0 0 4px; }
.login-card p { margin: 0 0 16px; color: var(--muted); }
.login-card form { display: grid; gap: 12px; }

.loading { color: var(--muted); padding: 24px; }

@media (max-width: 640px) {
  .topbar { padding: 10px 16px; gap: 10px; }
  .user { display: none; }
  main { padding: 12px 16px 32px; }
  .view-head h1 { min-width: 0; width: 100%; font-size: 18px; }
  .month .hide-sm { display: none; }
  .month th, .month td { padding: 7px 6px; }
  tr.detail > td { padding: 0 6px 10px; }
  .entries { display: block; overflow-x: auto; }
  .row { grid-template-columns: 1fr 1fr; }
  .row .field:first-child { grid-column: 1 / -1; }
}
