:root {
  --bg: #faf6f1;
  --surface: #ffffff;
  --surface-2: #f3ece4;
  --text: #2a211b;
  --muted: #7a6c61;
  --border: #e6dcd1;
  --accent: #c2562b;
  --accent-text: #ffffff;
  --accent-soft: #f8e3d8;
  --good: #2f7d4f;
  --good-soft: #dff1e5;
  --warn: #9a5b00;
  --warn-soft: #fbecd0;
  --danger: #b3261e;
  --shadow: 0 1px 2px rgba(60, 40, 20, .06), 0 2px 8px rgba(60, 40, 20, .06);
  --radius: 14px;
  color-scheme: light;
}
@media (prefers-color-scheme: dark) {
  :root {
    --bg: #1a1613;
    --surface: #25201c;
    --surface-2: #2f2924;
    --text: #f1e9e1;
    --muted: #a8998c;
    --border: #3a322b;
    --accent: #e0774a;
    --accent-text: #1a1613;
    --accent-soft: #43291d;
    --good: #6cc792;
    --good-soft: #1f3a2a;
    --warn: #f0b458;
    --warn-soft: #3d2f18;
    --danger: #ff8a80;
    --shadow: none;
    color-scheme: dark;
  }
}

* { box-sizing: border-box; }
html { -webkit-text-size-adjust: 100%; }
body {
  margin: 0;
  background: var(--bg);
  color: var(--text);
  font: 16px/1.45 system-ui, -apple-system, "Segoe UI", Roboto, sans-serif;
  padding-bottom: env(safe-area-inset-bottom);
}
a { color: inherit; }
img { display: block; max-width: 100%; }
h1, h2, h3 { line-height: 1.2; margin: 0; }
button, input, select, textarea { font: inherit; color: inherit; }

/* ---------- layout ---------- */
.topbar {
  position: sticky; top: 0; z-index: 10;
  background: color-mix(in srgb, var(--bg) 88%, transparent);
  backdrop-filter: blur(10px);
  -webkit-backdrop-filter: blur(10px);
  border-bottom: 1px solid var(--border);
  padding: calc(10px + env(safe-area-inset-top)) 16px 10px;
}
.topbar-row { display: flex; align-items: center; gap: 10px; max-width: 1100px; margin: 0 auto; }
.topbar h1 { font-size: 20px; flex: 1; min-width: 0; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.brand { display: flex; align-items: center; gap: 8px; text-decoration: none; }
.brand img { width: 28px; height: 28px; }
main { max-width: 1100px; margin: 0 auto; padding: 16px; }
.narrow { max-width: 680px; }

/* ---------- buttons & inputs ---------- */
.btn {
  display: inline-flex; align-items: center; justify-content: center; gap: 6px;
  min-height: 42px; padding: 0 16px;
  border: 1px solid var(--border); border-radius: 999px;
  background: var(--surface); cursor: pointer; text-decoration: none;
  font-weight: 550; white-space: nowrap;
}
.btn:hover { background: var(--surface-2); }
.btn.primary { background: var(--accent); border-color: var(--accent); color: var(--accent-text); }
.btn.primary:hover { filter: brightness(1.05); }
.btn.danger { color: var(--danger); }
.btn.small { min-height: 34px; padding: 0 12px; font-size: 14px; }
.btn.icon { width: 42px; padding: 0; }
.btn:disabled { opacity: .5; cursor: default; }

input[type=text], input[type=search], input[type=number], input[type=date], input[type=password], select, textarea {
  width: 100%; min-height: 44px; padding: 10px 12px;
  border: 1px solid var(--border); border-radius: 10px;
  background: var(--surface);
}
textarea { min-height: 80px; resize: vertical; }
input:focus, select:focus, textarea:focus, .chip-input:focus-within {
  outline: 2px solid var(--accent); outline-offset: -1px; border-color: transparent;
}
.field { display: block; margin-bottom: 14px; }
.field > span, .field-label { display: block; font-size: 14px; font-weight: 600; margin-bottom: 5px; color: var(--muted); }
.row { display: flex; gap: 12px; }
.row > * { flex: 1; min-width: 0; }

/* ---------- search & filters ---------- */
.search { position: relative; flex: 1; }
.search input { padding-left: 38px; border-radius: 999px; }
.search svg { position: absolute; left: 13px; top: 50%; transform: translateY(-50%); color: var(--muted); pointer-events: none; }
.filters { display: flex; flex-wrap: wrap; gap: 8px; align-items: center; margin-bottom: 12px; }
.filters select { width: auto; min-height: 36px; padding: 4px 30px 4px 12px; border-radius: 999px; font-size: 14px; }
.filter-group { display: inline-flex; gap: 6px; }
.segmented { display: inline-flex; background: var(--surface-2); border-radius: 999px; padding: 3px; }
.segmented button {
  border: 0; background: none; border-radius: 999px; padding: 5px 12px; font-size: 14px; cursor: pointer; color: var(--muted);
}
.segmented button.on { background: var(--surface); color: var(--text); font-weight: 600; box-shadow: var(--shadow); }
.tag-strip { display: flex; gap: 6px; overflow-x: auto; padding-bottom: 6px; margin-bottom: 10px; scrollbar-width: thin; }
.count { color: var(--muted); font-size: 14px; margin: 4px 0 12px; }

/* ---------- tags / chips ---------- */
.tag {
  display: inline-flex; align-items: center; gap: 4px;
  padding: 3px 10px; border-radius: 999px;
  background: var(--surface-2); border: 1px solid transparent;
  font-size: 13px; white-space: nowrap; text-decoration: none; cursor: pointer;
}
.tag.on { background: var(--accent); color: var(--accent-text); }
.tag .n { opacity: .6; font-size: 12px; }
.tag button { border: 0; background: none; padding: 0 0 0 2px; cursor: pointer; font-size: 15px; line-height: 1; opacity: .7; }
.tags { display: flex; flex-wrap: wrap; gap: 6px; }

.chip-input {
  display: flex; flex-wrap: wrap; gap: 6px; align-items: center;
  min-height: 44px; padding: 6px 8px;
  border: 1px solid var(--border); border-radius: 10px; background: var(--surface);
}
.chip-input input { flex: 1; min-width: 120px; border: 0; outline: 0; background: none; padding: 4px; min-height: 30px; }
.chip-input .tag { background: var(--accent-soft); cursor: default; }

/* ---------- puzzle grid ---------- */
.grid { display: grid; grid-template-columns: repeat(auto-fill, minmax(160px, 1fr)); gap: 12px; }
@media (min-width: 700px) { .grid { grid-template-columns: repeat(auto-fill, minmax(210px, 1fr)); gap: 16px; } }
.card {
  background: var(--surface); border: 1px solid var(--border); border-radius: var(--radius);
  overflow: hidden; text-decoration: none; display: flex; flex-direction: column;
  box-shadow: var(--shadow); transition: transform .12s;
}
.card:hover { transform: translateY(-2px); }
.card .thumb { aspect-ratio: 4 / 3; background: var(--surface-2); position: relative; overflow: hidden; }
.card .thumb img { position: absolute; inset: 0; width: 100%; height: 100%; object-fit: cover; }
/* Portrait puzzles: show the whole picture upright instead of cropping it into a landscape tile */
.card .thumb img.portrait { object-fit: contain; }
.card .body { padding: 10px 12px 12px; display: flex; flex-direction: column; gap: 3px; flex: 1; }
.card h3 { font-size: 15px; font-weight: 650; display: -webkit-box; -webkit-line-clamp: 2; -webkit-box-orient: vertical; overflow: hidden; }
.card .meta { font-size: 13px; color: var(--muted); }
.card .best { font-size: 13px; font-weight: 600; font-variant-numeric: tabular-nums; margin-top: auto; padding-top: 4px; }
.badge {
  display: inline-block; padding: 2px 8px; border-radius: 999px; font-size: 12px; font-weight: 600;
  background: var(--warn-soft); color: var(--warn);
}
.card .badge { position: absolute; top: 8px; left: 8px; }
.placeholder { width: 100%; height: 100%; display: grid; place-items: center; color: var(--muted); opacity: .5; }

.fab {
  position: fixed; right: 18px; bottom: calc(18px + env(safe-area-inset-bottom)); z-index: 20;
  width: 58px; height: 58px; border-radius: 50%; border: 0;
  background: var(--accent); color: var(--accent-text);
  display: grid; place-items: center; box-shadow: 0 4px 14px rgba(0,0,0,.25);
  text-decoration: none;
}

.empty { text-align: center; color: var(--muted); padding: 48px 16px; }
.empty .btn { margin-top: 12px; }

/* ---------- detail ---------- */
.detail { display: grid; gap: 20px; }
@media (min-width: 860px) { .detail { grid-template-columns: minmax(0, 5fr) minmax(0, 6fr); align-items: start; } }
.hero { border-radius: var(--radius); overflow: hidden; background: var(--surface-2); cursor: zoom-in; }
.hero img { width: 100%; max-height: 70vh; object-fit: contain; }
.hero .placeholder { aspect-ratio: 4 / 3; }
.panel { background: var(--surface); border: 1px solid var(--border); border-radius: var(--radius); padding: 16px; box-shadow: var(--shadow); }
.panel + .panel { margin-top: 16px; }
.panel h2 { font-size: 17px; margin-bottom: 12px; display: flex; align-items: center; gap: 8px; }
.panel h2 .spacer { flex: 1; }
.title-block h1 { font-size: 24px; margin-bottom: 4px; }
.title-block .meta { color: var(--muted); margin-bottom: 10px; }
.notes { white-space: pre-wrap; margin: 12px 0 0; }

.status-grid { display: grid; gap: 10px; }
.status-line { display: flex; align-items: center; gap: 10px; }
.status-line .label { color: var(--muted); font-size: 14px; width: 80px; flex: none; }
.status-line .value { flex: 1; font-weight: 550; }
.rented-box { background: var(--warn-soft); color: var(--warn); border-radius: 10px; padding: 10px 12px; }
.home-box { background: var(--good-soft); color: var(--good); border-radius: 10px; padding: 10px 12px; }

.times { list-style: none; margin: 0; padding: 0; }
.times li { display: flex; align-items: center; gap: 12px; padding: 10px 0; border-top: 1px solid var(--border); }
.times li:first-child { border-top: 0; }
.times .rank { width: 26px; text-align: center; color: var(--muted); font-size: 14px; font-variant-numeric: tabular-nums; }
.times .time { font-size: 18px; font-weight: 700; font-variant-numeric: tabular-nums; min-width: 78px; }
.times .who { flex: 1; min-width: 0; }
.times .who .names { font-weight: 550; }
.times .who .sub { font-size: 13px; color: var(--muted); }
.times li.pb .time { color: var(--accent); }
.times li.pb .rank { color: var(--accent); }
.times .edit { opacity: .6; }

.add-time { background: var(--surface-2); border-radius: 12px; padding: 14px; margin-bottom: 14px; }
.add-time .actions { display: flex; gap: 8px; justify-content: flex-end; }
.hint { font-size: 13px; color: var(--muted); margin-top: 4px; }

/* ---------- form ---------- */
.photo-pick {
  position: relative; border: 2px dashed var(--border); border-radius: var(--radius);
  aspect-ratio: 4 / 3; display: grid; place-items: center; overflow: hidden; cursor: pointer;
  background: var(--surface); margin-bottom: 16px; color: var(--muted); text-align: center;
}
.photo-pick img { position: absolute; inset: 0; width: 100%; height: 100%; object-fit: contain; background: var(--surface-2); }
.photo-actions { display: flex; flex-wrap: wrap; gap: 8px; margin: -6px 0 18px; }
.photo-edit { display: inline-flex; gap: 8px; margin-left: auto; }
.photo-edit .btn.icon { width: 34px; font-size: 18px; }
.form-actions {
  position: sticky; bottom: 0; background: var(--bg); padding: 12px 0 calc(12px + env(safe-area-inset-bottom));
  display: flex; gap: 10px; justify-content: flex-end; border-top: 1px solid var(--border); margin-top: 8px;
}
fieldset { border: 1px solid var(--border); border-radius: var(--radius); padding: 14px 14px 0; margin: 0 0 16px; }
legend { font-weight: 650; padding: 0 6px; }

/* ---------- login ---------- */
.login { min-height: 100vh; display: grid; place-items: center; padding: 16px; }
.login form { width: 100%; max-width: 340px; text-align: center; }
.login img { width: 72px; margin: 0 auto 12px; }
.login h1 { margin-bottom: 20px; }
.login .btn { width: 100%; margin-top: 10px; }
.error { color: var(--danger); font-size: 14px; min-height: 20px; margin-top: 8px; }

/* ---------- misc ---------- */
#toast {
  position: fixed; left: 50%; bottom: calc(90px + env(safe-area-inset-bottom)); transform: translateX(-50%) translateY(20px);
  background: var(--text); color: var(--bg); padding: 10px 18px; border-radius: 999px; font-size: 14px;
  opacity: 0; pointer-events: none; transition: .2s; z-index: 100; max-width: calc(100% - 32px);
}
#toast.show { opacity: 1; transform: translateX(-50%); }
.lightbox { position: fixed; inset: 0; background: rgba(0,0,0,.92); z-index: 200; display: grid; place-items: center; cursor: zoom-out; }
.lightbox img { max-width: 100%; max-height: 100%; object-fit: contain; }
dialog { border: 0; border-radius: var(--radius); background: var(--surface); color: var(--text); padding: 18px; width: min(420px, calc(100% - 32px)); }
dialog::backdrop { background: rgba(0,0,0,.45); }
dialog h3 { margin-bottom: 14px; }
dialog .actions { display: flex; gap: 8px; justify-content: flex-end; margin-top: 6px; }
.loading { text-align: center; color: var(--muted); padding: 40px; }
.hidden { display: none !important; }
