:root {
  --bg: #f6f3ee;
  --card: #ffffff;
  --ink: #1d2422;
  --muted: #6b7470;
  --line: #e4ded4;
  --brand: #1f3a34;
  --brand-2: #2f5a50;
  --accent: #e0803a;
  --good: #2e7d4f;
  --warn: #b7791f;
  --bad: #b3261e;
  --chip: #eef2ef;
  --radius: 16px;
  --shadow: 0 1px 2px rgba(29, 36, 34, .06), 0 4px 16px rgba(29, 36, 34, .05);
  --safe-b: env(safe-area-inset-bottom, 0px);
  font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, "Helvetica Neue", Arial, sans-serif;
  color: var(--ink);
  background: var(--bg);
  -webkit-text-size-adjust: 100%;
}
* { box-sizing: border-box; }
html, body { margin: 0; min-height: 100%; background: var(--bg); }
body { overscroll-behavior-y: none; }
a { color: var(--brand-2); }
button, input, select, textarea { font: inherit; color: inherit; }

/* ---------- top bar ---------- */
.topbar {
  position: sticky; top: 0; z-index: 20;
  display: flex; align-items: center; gap: 8px;
  padding: calc(env(safe-area-inset-top, 0px) + 10px) 14px 10px;
  background: rgba(246, 243, 238, .92);
  backdrop-filter: saturate(1.4) blur(10px);
  -webkit-backdrop-filter: saturate(1.4) blur(10px);
  border-bottom: 1px solid var(--line);
}
.topbar h1 { font-size: 19px; margin: 0; flex: 1; letter-spacing: -.01em; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.iconbtn {
  border: 0; background: transparent; padding: 8px; border-radius: 12px; cursor: pointer;
  display: inline-flex; align-items: center; justify-content: center; min-width: 40px; min-height: 40px;
}
.iconbtn:active { background: var(--chip); }
.iconbtn svg { width: 22px; height: 22px; }
.saved { font-size: 12px; color: var(--muted); min-width: 52px; text-align: right; }

main { padding: 14px 14px calc(120px + var(--safe-b)); max-width: 720px; margin: 0 auto; }

/* ---------- cards ---------- */
.card { background: var(--card); border-radius: var(--radius); box-shadow: var(--shadow); padding: 16px; margin-bottom: 14px; }
.card h2 { font-size: 13px; text-transform: uppercase; letter-spacing: .08em; color: var(--muted); margin: 0 0 12px; font-weight: 650; display: flex; align-items: center; gap: 8px; }
.card h2 .spacer { flex: 1; }
.card h2 svg { width: 15px; height: 15px; color: var(--accent); }
.hint { color: var(--muted); font-size: 13px; line-height: 1.4; margin: 6px 0 0; }

/* ---------- fields ---------- */
.field { margin-bottom: 14px; }
.field:last-child { margin-bottom: 0; }
.field label { display: flex; justify-content: space-between; font-size: 14px; font-weight: 600; margin-bottom: 6px; }
.field label .req { color: var(--accent); }
.field label .count { font-weight: 400; color: var(--muted); font-variant-numeric: tabular-nums; }
.field label .count.over { color: var(--bad); font-weight: 600; }
input[type=text], input[type=number], input[type=password], input[type=search], select, textarea {
  width: 100%; padding: 12px 13px; border: 1px solid var(--line); border-radius: 12px; background: #fcfbf9;
  font-size: 16px; outline: none; transition: border-color .15s, box-shadow .15s; appearance: none; -webkit-appearance: none;
}
select { background-image: linear-gradient(45deg, transparent 50%, var(--muted) 50%), linear-gradient(135deg, var(--muted) 50%, transparent 50%);
  background-position: calc(100% - 18px) 52%, calc(100% - 13px) 52%; background-size: 5px 5px; background-repeat: no-repeat; padding-right: 34px; }
textarea { min-height: 90px; resize: vertical; line-height: 1.45; }
textarea.tall { min-height: 200px; }
input:focus, select:focus, textarea:focus { border-color: var(--brand-2); box-shadow: 0 0 0 3px rgba(47, 90, 80, .15); background: #fff; }
.row { display: flex; gap: 10px; }
.row > * { flex: 1; min-width: 0; }
.row.tight > * { flex: 0 1 auto; }
.unit { position: relative; }
.unit input { padding-right: 40px; }
.unit span { position: absolute; right: 12px; top: 50%; transform: translateY(-50%); color: var(--muted); font-size: 14px; pointer-events: none; }
.money { position: relative; }
.money input { padding-left: 28px; font-size: 20px; font-weight: 650; }
.money span { position: absolute; left: 13px; top: 50%; transform: translateY(-50%); color: var(--muted); font-size: 18px; }

/* ---------- buttons ---------- */
.btn {
  display: inline-flex; align-items: center; justify-content: center; gap: 8px;
  border: 0; border-radius: 14px; padding: 13px 16px; font-weight: 650; font-size: 16px; cursor: pointer;
  background: var(--chip); color: var(--brand); min-height: 48px; text-decoration: none; position: relative;
  transition: transform .06s, opacity .15s, background .15s;
}
.btn:active { transform: scale(.98); }
.btn.primary { background: var(--brand); color: #fff; }
.btn.accent { background: var(--accent); color: #fff; }
.btn.ghost { background: transparent; border: 1px solid var(--line); }
.btn.small { min-height: 36px; padding: 7px 12px; font-size: 14px; border-radius: 10px; }
.btn.block { width: 100%; }
.btn.danger { color: var(--bad); }
.btn[disabled] { opacity: .55; pointer-events: none; }
.btn.busy { color: transparent !important; pointer-events: none; }
.btn.busy::after {
  content: ""; position: absolute; width: 20px; height: 20px; border-radius: 50%;
  border: 2.5px solid rgba(255,255,255,.5); border-top-color: #fff; animation: spin .8s linear infinite;
}
.btn:not(.primary):not(.accent).busy::after { border-color: rgba(31,58,52,.25); border-top-color: var(--brand); }
@keyframes spin { to { transform: rotate(360deg); } }
.btn svg { width: 20px; height: 20px; flex: none; }
.btns { display: flex; gap: 10px; flex-wrap: wrap; }
.btns > .btn { flex: 1; }

.seg { display: flex; background: var(--chip); padding: 4px; border-radius: 13px; gap: 4px; }
.seg button { flex: 1; border: 0; background: transparent; padding: 10px; border-radius: 10px; font-weight: 600; cursor: pointer; color: var(--muted); }
.seg button.on { background: #fff; color: var(--ink); box-shadow: var(--shadow); }

.toggle { display: flex; align-items: center; justify-content: space-between; gap: 12px; padding: 4px 0; font-weight: 600; font-size: 15px; }
.toggle input { display: none; }
.toggle .sw { width: 50px; height: 30px; border-radius: 30px; background: #d6d1c8; position: relative; transition: background .2s; flex: none; cursor: pointer; }
.toggle .sw::after { content: ""; position: absolute; top: 3px; left: 3px; width: 24px; height: 24px; border-radius: 50%; background: #fff; transition: transform .2s; box-shadow: 0 1px 3px rgba(0,0,0,.2); }
.toggle input:checked + .sw { background: var(--good); }
.toggle input:checked + .sw::after { transform: translateX(20px); }

/* ---------- list view ---------- */
.newbtn { width: 100%; margin-bottom: 16px; min-height: 58px; font-size: 17px; border-radius: 18px; }
.filters { display: flex; gap: 8px; margin-bottom: 12px; }
.filters button { border: 1px solid var(--line); background: #fff; border-radius: 20px; padding: 7px 14px; font-size: 14px; font-weight: 600; color: var(--muted); cursor: pointer; }
.filters button.on { background: var(--brand); border-color: var(--brand); color: #fff; }
.items { display: grid; gap: 10px; }
.itemrow { display: flex; gap: 12px; align-items: center; background: var(--card); border-radius: 16px; padding: 10px; box-shadow: var(--shadow); text-decoration: none; color: inherit; }
.itemrow .th { width: 68px; height: 68px; border-radius: 12px; background: var(--chip) center/cover no-repeat; flex: none; display: flex; align-items: center; justify-content: center; color: var(--muted); }
.itemrow .meta { flex: 1; min-width: 0; }
.itemrow .t { font-weight: 600; font-size: 15px; line-height: 1.3; display: -webkit-box; -webkit-line-clamp: 2; -webkit-box-orient: vertical; overflow: hidden; }
.itemrow .s { color: var(--muted); font-size: 13px; margin-top: 4px; display: flex; gap: 8px; align-items: center; }
.chip { display: inline-flex; align-items: center; gap: 4px; padding: 3px 9px; border-radius: 20px; font-size: 12px; font-weight: 650; background: var(--chip); color: var(--brand); white-space: nowrap; }
.chip.listed { background: #e3f1e8; color: var(--good); }
.chip.exact { background: #e3f1e8; color: var(--good); }
.chip.close { background: #e8eff6; color: #2b5f8e; }
.chip.similar { background: #f6eedf; color: var(--warn); }
.chip.low { background: #fbe9e7; color: var(--bad); }
.chip.sold { background: #e3f1e8; color: var(--good); }
.chip.asking { background: #eeeae4; color: #6b5f50; }
.soldshots { background: var(--chip); border-radius: 12px; padding: 10px 12px 12px; margin: 4px 0 12px; }
.soldshots .lbl { font-weight: 700; font-size: 14px; }
.soldshots .btn { display: inline-flex; align-items: center; gap: 6px; justify-content: center; }
.soldshots .btn svg { width: 16px; height: 16px; }
.photos.shots { grid-template-columns: repeat(4, 1fr); margin-bottom: 0; }
.photos.shots .photo { aspect-ratio: 9 / 16; }
.photos.shots .photo img { object-position: top; }
.comp .th svg { width: 28px; height: 28px; margin: 18px; color: var(--muted); }
.empty { text-align: center; color: var(--muted); padding: 40px 20px; }
.empty svg { width: 54px; height: 54px; opacity: .4; margin-bottom: 8px; }

/* ---------- photos ---------- */
.photos { display: grid; grid-template-columns: repeat(3, 1fr); gap: 8px; margin-bottom: 12px; }
.photo { position: relative; aspect-ratio: 1; border-radius: 12px; overflow: hidden; background: var(--chip); border: 0; padding: 0; cursor: pointer; }
.photo img { width: 100%; height: 100%; object-fit: cover; display: block; }
.photo .badge { position: absolute; left: 6px; top: 6px; background: var(--brand); color: #fff; font-size: 11px; font-weight: 700; padding: 2px 7px; border-radius: 8px; }
.photo.uploading::after { content: ""; position: absolute; inset: 0; background: rgba(255,255,255,.55); }
.photo.uploading::before { content: ""; position: absolute; z-index: 1; left: calc(50% - 12px); top: calc(50% - 12px); width: 24px; height: 24px; border-radius: 50%; border: 3px solid rgba(31,58,52,.25); border-top-color: var(--brand); animation: spin .8s linear infinite; }
.photo.sel { outline: 3px solid var(--accent); outline-offset: -3px; }
.photoactions { display: flex; gap: 8px; margin: -4px 0 12px; }
.photoactions .btn { flex: 1; }

/* ---------- AI panel ---------- */
.ai-box { background: linear-gradient(135deg, #f3f7f5, #fbf7f1); border: 1px solid var(--line); border-radius: 14px; padding: 12px 14px; margin-top: 12px; font-size: 14px; line-height: 1.45; }
.ai-box ul { margin: 6px 0 0; padding-left: 20px; }
.ai-box li { margin: 3px 0; }
.ai-box .lbl { font-weight: 700; font-size: 13px; color: var(--brand); margin-top: 8px; }
.ai-box .lbl:first-child { margin-top: 0; }
.note-warn { color: var(--warn); }

/* ---------- categories ---------- */
.catcurrent { display: flex; align-items: center; gap: 10px; padding: 12px 13px; border: 1px solid var(--line); border-radius: 12px; background: #fcfbf9; }
.catcurrent .n { flex: 1; min-width: 0; }
.catcurrent .n b { display: block; font-size: 15px; }
.catcurrent .n small { color: var(--muted); font-size: 12px; display: block; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.catlist { margin-top: 8px; display: grid; gap: 6px; }
.catopt { text-align: left; border: 1px solid var(--line); background: #fff; border-radius: 12px; padding: 10px 12px; cursor: pointer; }
.catopt b { display: block; font-size: 14px; }
.catopt small { color: var(--muted); font-size: 12px; }
.catopt.on { border-color: var(--brand-2); background: #f1f6f4; }

details.more { margin-top: 6px; }
details.more summary { cursor: pointer; color: var(--brand-2); font-weight: 600; font-size: 14px; padding: 8px 0; list-style: none; }
details.more summary::-webkit-details-marker { display: none; }
details.more summary::before { content: "▸ "; }
details.more[open] summary::before { content: "▾ "; }

/* ---------- comps ---------- */
.pricebox { display: grid; grid-template-columns: 1.3fr 1fr 1fr; gap: 8px; margin: 12px 0; }
.pricecell { background: var(--chip); border-radius: 14px; padding: 10px; text-align: center; border: 0; cursor: pointer; }
.pricecell.main { background: var(--brand); color: #fff; }
.pricecell .v { font-size: 22px; font-weight: 750; letter-spacing: -.02em; font-variant-numeric: tabular-nums; }
.pricecell.main .v { font-size: 26px; }
.pricecell .k { font-size: 11px; text-transform: uppercase; letter-spacing: .06em; opacity: .75; margin-top: 2px; }
.pricecell .use { font-size: 12px; margin-top: 6px; font-weight: 650; opacity: .9; text-decoration: underline; }
.statline { font-size: 13px; color: var(--muted); margin-bottom: 10px; font-variant-numeric: tabular-nums; }
.comp { display: flex; gap: 10px; padding: 10px 0; border-top: 1px solid var(--line); text-decoration: none; color: inherit; }
.comp:first-of-type { border-top: 0; }
.comp .th { width: 64px; height: 64px; border-radius: 10px; background: var(--chip) center/cover no-repeat; flex: none; }
.comp .meta { flex: 1; min-width: 0; }
.comp .t { font-size: 14px; line-height: 1.3; font-weight: 550; display: -webkit-box; -webkit-line-clamp: 2; -webkit-box-orient: vertical; overflow: hidden; }
.comp .p { font-weight: 750; font-size: 16px; margin-top: 3px; font-variant-numeric: tabular-nums; }
.comp .p small { font-weight: 500; color: var(--muted); font-size: 12px; }
.comp .s { font-size: 12px; color: var(--muted); margin-top: 3px; display: flex; flex-wrap: wrap; gap: 6px; align-items: center; }
.comp .note { font-size: 12px; color: var(--brand-2); margin-top: 3px; }
.comp.dim { opacity: .6; }

/* ---------- results ---------- */
.result { border-radius: 14px; padding: 12px 14px; margin-top: 12px; font-size: 14px; line-height: 1.45; }
.result.ok { background: #e9f4ed; color: #1f5537; }
.result.err { background: #fbecea; color: #7d1c16; }
.result.info { background: #f6eedf; color: #6b4a12; }
.result ul { margin: 6px 0 0; padding-left: 20px; }
.result b { display: block; margin-bottom: 4px; font-size: 15px; }

/* ---------- bottom bar ---------- */
.bottombar {
  position: fixed; left: 0; right: 0; bottom: 0; z-index: 30;
  padding: 10px 14px calc(10px + var(--safe-b));
  background: rgba(255,255,255,.95); backdrop-filter: blur(10px); -webkit-backdrop-filter: blur(10px);
  border-top: 1px solid var(--line); display: flex; gap: 10px; justify-content: center;
}
.bottombar .btn { flex: 1; max-width: 340px; }

/* ---------- login ---------- */
.login { max-width: 380px; margin: 14vh auto 0; padding: 0 20px; text-align: center; }
.login img { width: 76px; height: 76px; margin-bottom: 14px; }
.login h1 { font-size: 24px; margin: 0 0 6px; }
.login p { color: var(--muted); margin: 0 0 22px; }
.login form { display: grid; gap: 12px; }

/* ---------- diagnostics ---------- */
.diag { display: grid; gap: 6px; font-size: 14px; }
.diag > div { display: flex; gap: 8px; align-items: flex-start; }
.diag .dot { width: 10px; height: 10px; border-radius: 50%; margin-top: 5px; flex: none; }
.diag .dot.ok { background: var(--good); }
.diag .dot.no { background: var(--bad); }
.diag small { color: var(--muted); display: block; word-break: break-word; }
.banner { background: #f6eedf; color: #6b4a12; border-radius: 12px; padding: 10px 12px; font-size: 13px; margin-bottom: 12px; }

/* ---------- toast + sheet ---------- */
#toast { position: fixed; left: 50%; bottom: calc(96px + var(--safe-b)); transform: translate(-50%, 20px); background: var(--ink); color: #fff; padding: 11px 16px; border-radius: 14px; font-size: 14px; opacity: 0; pointer-events: none; transition: all .25s; z-index: 60; max-width: calc(100% - 32px); box-shadow: 0 8px 24px rgba(0,0,0,.2); }
#toast.show { opacity: 1; transform: translate(-50%, 0); }
#toast.err { background: var(--bad); }
.sheet-bg { position: fixed; inset: 0; background: rgba(0,0,0,.35); z-index: 50; display: flex; align-items: flex-end; }
.sheet { background: #fff; width: 100%; border-radius: 20px 20px 0 0; padding: 18px 16px calc(18px + var(--safe-b)); display: grid; gap: 10px; animation: up .2s ease-out; max-width: 720px; margin: 0 auto; }
.sheet img { max-height: 46vh; width: 100%; object-fit: contain; border-radius: 12px; background: var(--chip); }
@keyframes up { from { transform: translateY(40px); opacity: .4; } }
.muted { color: var(--muted); }
.mt { margin-top: 12px; }
.hidden { display: none !important; }
