:root {
  color-scheme: dark;
  --bg: #0e1218;
  --bg-2: #131922;
  --panel: #1a212b;
  --panel-2: #202937;
  --text: #f1f4f8;
  --muted: #8d99aa;
  --accent: #efb84f;
  --accent-2: #e09a2c;
  --accent-text: #161a20;
  --line: rgba(255,255,255,.08);
  --line-strong: rgba(255,255,255,.14);
  --green: #45d6a0;
  --red: #ff7b80;
  font-family: "Segoe UI Variable", "Segoe UI", system-ui, sans-serif;
  font-size: 13px;
}

* { box-sizing: border-box; }
html, body { height: 100%; }
body { margin: 0; background: var(--bg); color: var(--text); overflow: hidden; user-select: none; }
button, input, select, textarea { font: inherit; }
button { color: inherit; }
input, textarea { user-select: text; }
::-webkit-scrollbar { width: 10px; height: 10px; }
::-webkit-scrollbar-thumb { border: 3px solid transparent; border-radius: 10px; background: rgba(255,255,255,.12); background-clip: padding-box; }
::-webkit-scrollbar-track { background: transparent; }

h1, h2, p { margin-top: 0; }
h1 { margin: 0; font-size: 15px; font-weight: 700; letter-spacing: -.01em; }
h2 { margin: 0; font-size: 18px; font-weight: 700; letter-spacing: -.02em; overflow-wrap: anywhere; }
.eyebrow { margin: 0 0 3px; color: var(--accent); font-size: 10px; font-weight: 700; letter-spacing: .14em; text-transform: uppercase; }

/* ---------- main window shell ---------- */
.app-shell {
  display: flex;
  flex-direction: column;
  height: 100vh;
  padding: 0 22px 14px;
  background: radial-gradient(ellipse at 100% 0%, rgba(239,184,79,.07), transparent 40%), var(--bg);
}

.topbar { display: flex; align-items: center; justify-content: space-between; gap: 16px; height: 56px; border-bottom: 1px solid var(--line); }
.brand { display: flex; align-items: center; gap: 10px; min-width: 0; }
.brand-mark { display: grid; width: 30px; height: 30px; place-items: center; border-radius: 9px; background: linear-gradient(135deg, #f7d27f, #e09a2c); color: #161a20; font-size: 15px; font-weight: 800; }
.brand h1 { white-space: nowrap; }
.header-actions { display: flex; align-items: center; gap: 4px; }
.icon-button { display: grid; width: 34px; height: 34px; place-items: center; border: 1px solid transparent; border-radius: 9px; background: transparent; color: var(--muted); font-size: 15px; cursor: pointer; transition: background .12s ease, color .12s ease; }
.icon-button:hover { background: var(--panel); color: var(--text); }
.icon-button:focus-visible, button:focus-visible, select:focus-visible { outline: 2px solid rgba(239,184,79,.6); outline-offset: 1px; }

/* ---------- navigation tabs ---------- */
.tabs-row { display: flex; align-items: center; justify-content: space-between; gap: 12px; padding: 10px 0; }
.tabs { display: flex; gap: 4px; }
.tab { height: 32px; padding: 0 14px; border: 1px solid transparent; border-radius: 8px; background: transparent; color: var(--muted); font-weight: 600; cursor: pointer; }
.tab:hover { color: var(--text); background: var(--panel); }
.tab.active { border-color: rgba(239,184,79,.3); background: rgba(239,184,79,.12); color: var(--accent); }

/* ---------- toolbar ---------- */
.filters { display: flex; align-items: center; gap: 8px; padding: 6px 0 10px; }
.filters input[type=search] { flex: 1 1 260px; min-width: 180px; }
.filter-row { display: flex; gap: 8px; }
.filter-row select, #period-filter { width: 170px; }
.filters select { width: 170px; }
.result-count { margin: 0 0 8px; min-height: 16px; color: var(--muted); font-size: 12px; }

input, select, textarea {
  width: 100%;
  min-height: 34px;
  border: 1px solid var(--line-strong);
  border-radius: 8px;
  padding: 7px 10px;
  outline: none;
  background: var(--bg-2);
  color: var(--text);
}
input:focus, select:focus, textarea:focus { border-color: rgba(239,184,79,.7); }
textarea { resize: vertical; }
input[type=file] { min-height: auto; padding: 8px; font-size: 12px; }
input[type=search] { min-height: 34px; }

.primary, .secondary, .danger {
  display: inline-flex; align-items: center; justify-content: center; gap: 6px;
  min-height: 34px; width: auto; padding: 7px 14px; border: 1px solid transparent; border-radius: 8px;
  font-weight: 600; cursor: pointer; transition: background .12s ease, border-color .12s ease;
}
.primary { background: linear-gradient(180deg, #f4c563, #e4a232); color: var(--accent-text); }
.primary:hover { filter: brightness(1.06); }
.primary:disabled { opacity: .6; cursor: default; }
.secondary { border-color: var(--line-strong); background: var(--panel-2); color: var(--text); }
.secondary:hover { background: #26303e; }
.danger { border-color: rgba(255,123,128,.3); background: rgba(255,123,128,.1); color: var(--red); }
.danger:hover { background: rgba(255,123,128,.18); }
.add-button, .add-button-top { white-space: nowrap; }
.add-button-top { flex: 0 0 auto; }
.button-stack { display: flex; flex-wrap: wrap; gap: 8px; }
.button-stack > * { flex: 1 1 auto; }

/* ---------- summary cards ---------- */
.summary { display: grid; grid-template-columns: repeat(4, minmax(0, 1fr)); gap: 10px; padding: 4px 0 12px; }
.summary-chip { display: grid; gap: 2px; min-width: 0; padding: 12px 14px; border: 1px solid var(--line); border-radius: 10px; background: var(--panel); }
.summary-chip strong { font-size: 18px; font-weight: 700; letter-spacing: -.02em; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.summary-chip span { color: var(--muted); font-size: 12px; }
.summary-chip:last-child strong { color: var(--green); }

/* ---------- record list (table-like rows) ---------- */
.record-list { display: flex; flex: 1 1 auto; flex-direction: column; gap: 4px; min-height: 0; overflow-y: auto; padding: 0 4px 0 0; }
.record-card {
  display: grid; grid-template-columns: 36px minmax(0, 1fr) auto; align-items: center; gap: 12px;
  width: 100%; min-height: 56px; padding: 8px 14px; border: 1px solid var(--line); border-radius: 10px;
  background: var(--panel); text-align: left; cursor: pointer; transition: background .12s ease, border-color .12s ease;
}
.record-card:hover { border-color: rgba(239,184,79,.3); background: var(--panel-2); }
.record-icon { display: grid; width: 36px; height: 36px; place-items: center; border-radius: 9px; background: rgba(239,184,79,.1); font-size: 16px; }
.record-main { min-width: 0; }
.record-name { display: block; overflow: hidden; font-weight: 600; text-overflow: ellipsis; white-space: nowrap; }
.record-sub { display: block; margin-top: 2px; color: var(--muted); font-size: 12px; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.record-right { display: grid; justify-items: end; gap: 2px; min-width: 150px; text-align: right; }
.record-price { font-size: 13px; font-weight: 700; white-space: nowrap; }
.record-status { color: var(--muted); font-size: 11px; white-space: nowrap; }
.profit { color: var(--green); }
.loss { color: var(--red); }

.empty, .loading, .error-panel { padding: 36px 16px; border: 1px dashed var(--line-strong); border-radius: 10px; color: var(--muted); text-align: center; }
.error-panel { color: var(--red); }

.undo-banner { display: flex; align-items: center; gap: 10px; margin: 0 0 8px; padding: 8px 12px; border: 1px solid rgba(239,184,79,.25); border-radius: 8px; background: rgba(239,184,79,.07); font-size: 12px; }
.undo-banner span { flex: 1; color: var(--muted); }
.undo-banner button { border: 0; border-radius: 6px; padding: 5px 10px; background: var(--panel-2); color: var(--text); font-weight: 600; cursor: pointer; }

.footer-note { margin: 8px 0 0; color: var(--muted); font-size: 11px; text-align: center; }

/* ---------- dialogs ---------- */
.sheet {
  width: min(640px, calc(100vw - 48px));
  max-height: min(86vh, 860px);
  padding: 0;
  border: 1px solid var(--line-strong);
  border-radius: 12px;
  background: var(--bg-2);
  color: var(--text);
  box-shadow: 0 24px 70px rgba(0,0,0,.5);
}
.sheet::backdrop { background: rgba(4,6,10,.6); }
.form { display: grid; gap: 14px; padding: 18px 22px 20px; overflow-y: auto; }
.sheet-heading { display: flex; align-items: center; justify-content: space-between; gap: 14px; margin-bottom: 2px; }
.sheet-heading .icon-button { width: 30px; height: 30px; }
label { display: grid; gap: 6px; color: var(--muted); font-size: 12px; font-weight: 600; }
.optional { color: var(--muted); font-size: 11px; font-weight: 400; }
.form-error { min-height: 0; margin: -4px 0 0; color: var(--red); font-size: 12px; }
.market-privacy, .sharing-card > span { margin: 0; color: var(--muted); font-size: 12px; line-height: 1.5; }
.sharing-card { display: grid; gap: 8px; padding: 14px; border: 1px solid var(--line); border-radius: 10px; background: var(--panel); }
.sharing-card strong { font-size: 13px; }
.settings-action { width: 100%; justify-content: flex-start; }
.market-filters { display: flex; gap: 8px; }
.market-filters select { width: auto; flex: 1; }
.market-summary { display: grid; grid-template-columns: repeat(4, minmax(0, 1fr)); gap: 8px; }
.market-records { display: flex; flex-direction: column; gap: 6px; max-height: 52vh; overflow-y: auto; }
.market-sale-card { display: grid; gap: 4px; padding: 12px 14px; border: 1px solid var(--line); border-radius: 10px; background: var(--panel); cursor: pointer; }
.market-sale-card:hover { border-color: rgba(239,184,79,.3); }
.market-sale-card strong { overflow-wrap: anywhere; font-size: 13px; }
.market-sale-card span { color: var(--muted); font-size: 12px; line-height: 1.4; }
.trash-card { display: flex; align-items: center; justify-content: space-between; gap: 12px; padding: 10px 14px; border: 1px solid var(--line); border-radius: 10px; background: var(--panel); }
.trash-card > div { display: grid; gap: 2px; min-width: 0; }
.trash-card strong { overflow-wrap: anywhere; }
.trash-card span { color: var(--muted); font-size: 12px; }
.detail-content { display: grid; gap: 0; }
.detail-line { display: flex; justify-content: space-between; gap: 16px; padding: 9px 0; border-bottom: 1px solid var(--line); font-size: 13px; }
.detail-line span:first-child { color: var(--muted); }
.detail-line span:last-child { text-align: right; overflow-wrap: anywhere; }
.detail-notes { padding: 12px; border: 1px solid var(--line); border-radius: 8px; background: var(--panel); font-size: 13px; white-space: pre-wrap; overflow-wrap: anywhere; }
.photo-strip { display: flex; gap: 8px; overflow-x: auto; padding: 2px 0 6px; scrollbar-width: thin; }
.photo-strip img { height: 180px; flex: 0 0 auto; border: 1px solid var(--line); border-radius: 8px; object-fit: contain; background: #080b10; cursor: zoom-in; }
.sale-form { display: grid; gap: 10px; padding-top: 6px; }
.calculator-result { display: block; padding: 14px; border: 1px solid rgba(239,184,79,.25); border-radius: 8px; background: rgba(239,184,79,.08); font-size: 15px; font-weight: 600; line-height: 1.55; white-space: pre-line; }

.photo-viewer { width: 100vw; max-width: none; height: 100vh; max-height: none; margin: 0; padding: 0; border: 0; background: rgba(4,6,9,.97); color: #fff; overflow: hidden; }
.photo-viewer::backdrop { background: #000; }
.photo-viewer > img { display: block; width: 100%; height: 100%; object-fit: contain; }
.photo-viewer-close, .photo-nav { position: absolute; z-index: 2; display: grid; place-items: center; width: 40px; height: 40px; border: 1px solid rgba(255,255,255,.18); border-radius: 50%; background: rgba(25,31,39,.85); color: #fff; font-size: 22px; cursor: pointer; }
.photo-viewer-close { top: 14px; right: 14px; }
.photo-nav { top: 50%; transform: translateY(-50%); }
.photo-prev { left: 14px; }
.photo-next { right: 14px; }
.photo-viewer-caption { position: absolute; right: 70px; bottom: 14px; left: 70px; margin: 0; color: rgba(255,255,255,.8); font-size: 13px; text-align: center; }

.login-dialog .form { padding: 26px 26px 24px; }
.login-dialog .form label { gap: 6px; }

.toast { position: fixed; z-index: 10; bottom: 22px; left: 50%; width: max-content; max-width: 80vw; padding: 9px 16px; border-radius: 8px; background: #e7eef7; color: #111a26; font-size: 13px; font-weight: 600; opacity: 0; pointer-events: none; transform: translate(-50%, 8px); transition: .18s ease; }
.toast.show { opacity: 1; transform: translate(-50%, 0); }

.hidden { display: none !important; }

/* ---------- compact overlay (used while playing) ---------- */
.overlay-controls { display: none; align-items: center; gap: 4px; }
body.overlay { background: #0e1218; overflow: hidden; }
body.overlay .app-shell { height: 100vh; padding: 0 12px 10px; background: #0e1218; border: 1px solid rgba(239,184,79,.35); border-radius: 12px; }
body.overlay .topbar { height: 46px; cursor: move; -webkit-app-region: drag; }
body.overlay .brand-mark { width: 24px; height: 24px; font-size: 12px; border-radius: 7px; }
body.overlay .brand h1 { font-size: 13px; }
body.overlay .brand .eyebrow, body.overlay .header-actions, body.overlay .summary, body.overlay .filters .filter-row,
body.overlay #period-filter, body.overlay #status-filter, body.overlay #sort-filter, body.overlay #class-filter,
body.overlay .add-button-top, body.overlay .result-count, body.overlay .footer-note, body.overlay .undo-banner { display: none !important; }
body.overlay .overlay-controls { display: flex; }
body.overlay .overlay-controls button, body.overlay .overlay-controls .icon-button { -webkit-app-region: no-drag; }
body.overlay .tabs { padding: 6px 0; }
body.overlay .tab { height: 28px; padding: 0 10px; font-size: 12px; }
body.overlay .filters { padding: 2px 0 8px; }
body.overlay .filters input[type=search] { min-width: 0; }
body.overlay .record-card { min-height: 44px; padding: 6px 10px; grid-template-columns: 26px minmax(0, 1fr) auto; gap: 8px; }
body.overlay .record-icon { width: 26px; height: 26px; font-size: 13px; }
body.overlay .record-right { min-width: 0; }
body.overlay .record-status { display: none; }
body.overlay .record-price { font-size: 12px; }
body.overlay .sheet { width: calc(100vw - 24px); }
body.overlay .toast { font-size: 12px; }

/* ---------- narrow window fallback ---------- */
@media (max-width: 900px) {
  .summary { grid-template-columns: repeat(2, minmax(0, 1fr)); }
  .filter-row select, .filters select, #period-filter { width: 140px; }
  .record-right { min-width: 110px; }
}
