/* The Grand Era — newspaper paper, black ink, mobile-first */
:root {
  --paper: #f4efe3;
  --paper-deep: #ebe3d3;
  --card: #fffaf2;
  --ink: #1c1916;
  --ink-soft: #5e584f;
  --ink-faint: #8a8378;
  --line: #d8ccb6;
  --line-strong: #1c1916;
  --red: #c0392b;
  --green: #1b7a45;
  /* International convention: green = up/gain, red = down/loss */
  --gold: #8a6a2a;
  --gold-bg: #f3e6c4;
  --safe-bg: #e4efdf;
  --safe-fg: #3a6b42;
  --warn-bg: #f6e6c4;
  --warn-fg: #8a5a10;
  --danger-bg: #f4d8d3;
  --danger-fg: #9a2c20;
  --shadow: 0 10px 28px rgba(40, 28, 8, 0.08);
  --radius: 14px;
  --serif: "Noto Serif CJK TC", "Noto Serif CJK HK", "Noto Serif CJK SC",
    "Source Han Serif TC", "Source Han Serif SC", "Songti TC", "Songti SC",
    "PMingLiU", "PMingLiu", "STSong", "SimSun", "Noto Serif", "Times New Roman",
    serif;
  --sans: "Noto Sans CJK TC", "Noto Sans CJK HK", "Noto Sans CJK SC",
    "Source Han Sans TC", "PingFang TC", "PingFang SC", "Microsoft JhengHei",
    "Microsoft YaHei", "Noto Sans", sans-serif;
}

* { box-sizing: border-box; }
html, body { margin: 0; padding: 0; }
html { height: 100%; }
body {
  min-height: 100%;
  background: var(--paper-deep);
  color: var(--ink);
  font-family: var(--serif);
  font-size: 16px;
  line-height: 1.45;
  -webkit-font-smoothing: antialiased;
  text-rendering: optimizeLegibility;
}

button, input {
  font-family: inherit;
  color: inherit;
}
button { cursor: pointer; }

#app {
  min-height: 100vh;
  min-height: 100dvh;
  display: flex;
  justify-content: center;
  background:
    radial-gradient(ellipse at top, rgba(255, 252, 245, 0.7), transparent 50%),
    var(--paper-deep);
}

.shell {
  width: 100%;
  max-width: 430px;
  min-height: 100vh;
  min-height: 100dvh;
  height: 100vh;
  height: 100dvh;
  overflow-x: hidden;
  overflow-y: auto;
  background-color: var(--paper);
  background-image:
    linear-gradient(180deg, rgba(255, 255, 255, 0.28), transparent 120px),
    repeating-linear-gradient(
      0deg,
      transparent,
      transparent 27px,
      rgba(120, 90, 40, 0.015) 28px
    );
  position: relative;
  box-shadow: 0 0 0 1px rgba(80, 60, 30, 0.06), 0 20px 50px rgba(40, 28, 8, 0.08);
}

.screen { display: none; min-height: 100%; }
.screen.active { display: block; }
.start-screen,
.end-screen { padding-top: 44px; }
.start-screen .masthead.framed,
.end-screen .masthead.framed { margin-top: 8px; }

/* Language switcher */
.lang-switch {
  position: absolute;
  top: 14px;
  right: 16px;
  z-index: 5;
  display: flex;
  align-items: center;
  gap: 0;
  border: 1px solid var(--ink);
  border-radius: 999px;
  overflow: hidden;
  background: var(--card);
  height: 28px;
}
.lang-switch button {
  border: 0;
  background: transparent;
  min-width: 32px;
  height: 28px;
  padding: 0 9px;
  font-size: 12px;
  letter-spacing: 0.02em;
  color: var(--ink);
}
.lang-switch button[aria-pressed="true"] {
  background: var(--ink);
  color: var(--paper);
}
.lang-switch button:focus-visible {
  outline: 2px solid var(--ink);
  outline-offset: -2px;
}

/* Masthead */
.masthead {
  text-align: center;
  padding: 36px 20px 4px;
}
.main-screen .masthead {
  padding: 28px 20px 2px;
}
.masthead h1 {
  margin: 0;
  font-size: 48px;
  font-weight: 700;
  letter-spacing: 0.08em;
  line-height: 1.1;
}
.main-screen .masthead h1 { font-size: 44px; }
.masthead .rule-line {
  display: flex;
  align-items: center;
  justify-content: center;
  gap: 10px;
  margin-top: 6px;
  color: var(--ink);
}
.masthead .rule-line::before,
.masthead .rule-line::after {
  content: "";
  width: 64px;
  height: 1px;
  background: var(--ink);
}
.masthead .subtitle {
  font-size: 13px;
  letter-spacing: 0.12em;
  font-style: italic;
  font-weight: 400;
}

.masthead.framed {
  margin: 28px 18px 8px;
  padding: 22px 16px 18px;
  border: 2px solid var(--ink);
  box-shadow: inset 0 0 0 1px var(--ink);
  position: relative;
}
.masthead.framed h1 { font-size: 56px; }
.lang-on-frame {
  position: absolute;
  top: 10px;
  right: 12px;
  font-size: 11px;
  letter-spacing: 0.04em;
}

/* Start */
.start-body {
  padding: 12px 28px 40px;
  text-align: center;
}
.start-body .intro,
.start-body .rules {
  text-align: left;
  color: var(--ink);
  font-size: 14.5px;
  line-height: 1.7;
  margin: 16px 0 0;
}
.start-body .rules {
  color: var(--ink-soft);
  font-size: 13px;
  line-height: 1.7;
}
.btn-primary {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  width: 100%;
  min-height: 52px;
  margin-top: 28px;
  background: var(--ink);
  color: var(--paper);
  border: 0;
  border-radius: 4px;
  font-size: 18px;
  letter-spacing: 0.18em;
  font-weight: 600;
}
.btn-primary:active { transform: translateY(1px); }
.btn-primary:focus-visible {
  outline: 3px solid var(--ink);
  outline-offset: 3px;
}
.btn-ghost {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  width: 100%;
  min-height: 44px;
  margin-top: 8px;
  background: transparent;
  border: 0;
  color: var(--ink);
  font-size: 16px;
  text-decoration: underline;
  text-underline-offset: 4px;
}

/* Status row */
.status-panel {
  display: grid;
  grid-template-columns: 76px 1fr 108px;
  align-items: start;
  column-gap: 8px;
  padding: 10px 14px 12px;
  border-bottom: 1px solid var(--line);
}
.status-panel .week {
  font-size: 12px;
  color: var(--ink-soft);
  padding-top: 22px;
  white-space: nowrap;
}
.status-panel .assets {
  text-align: center;
  min-width: 0;
  padding: 0 4px;
}
.status-panel .assets .label {
  font-size: 11px;
  color: var(--ink-soft);
  letter-spacing: 0.16em;
}
.status-panel .assets .value {
  font-size: 32px;
  font-weight: 700;
  letter-spacing: -0.02em;
  line-height: 1.05;
  font-variant-numeric: tabular-nums;
  margin-top: 1px;
}
.status-panel .assets .pill { margin-top: 6px; }
.cash-pp {
  display: flex;
  justify-content: flex-end;
  gap: 10px;
  padding-top: 4px;
  text-align: right;
}
.cash-pp .item .label {
  font-size: 11px;
  color: var(--ink-soft);
  letter-spacing: 0.06em;
  line-height: 1.2;
}
.cash-pp .item .value {
  font-size: 13px;
  font-weight: 600;
  font-variant-numeric: tabular-nums;
  white-space: nowrap;
}
.pill {
  display: inline-flex;
  align-items: center;
  gap: 6px;
  border-radius: 999px;
  padding: 3px 11px;
  font-size: 11px;
  letter-spacing: 0.08em;
}
.pill.safe { background: var(--safe-bg); color: var(--safe-fg); }
.pill.warning { background: var(--warn-bg); color: var(--warn-fg); }
.pill.danger { background: var(--danger-bg); color: var(--danger-fg); }

/* Sections */
.section {
  padding: 12px 16px 6px;
}
.section-title {
  display: flex;
  align-items: center;
  gap: 8px;
  font-size: 14px;
  font-weight: 700;
  margin: 0 0 8px;
  letter-spacing: 0.04em;
}
.section-title::before {
  content: "◆";
  font-size: 11px;
}

/* News cards */
.news-card {
  background: var(--card);
  border: 1px solid var(--line);
  border-radius: 12px;
  padding: 11px 12px 10px;
  margin-bottom: 8px;
  box-shadow: 0 1px 0 rgba(255, 255, 255, 0.7) inset;
}
.news-card.record {
  background: transparent;
  border-style: dashed;
}
.news-card h3 {
  margin: 0 0 6px;
  font-size: 15.5px;
  line-height: 1.4;
  font-weight: 700;
}
.news-card p {
  margin: 0 0 10px;
  font-size: 13px;
  color: var(--ink-soft);
  line-height: 1.65;
}
.news-card .top-row {
  display: flex;
  align-items: flex-start;
  justify-content: space-between;
  gap: 8px;
}
.tags {
  display: flex;
  flex-wrap: wrap;
  gap: 6px;
  align-items: center;
}
.tag {
  display: inline-flex;
  align-items: center;
  border: 1px solid var(--line);
  border-radius: 999px;
  padding: 2px 9px;
  font-size: 11px;
  color: var(--ink-soft);
  background: #f7f1e6;
  letter-spacing: 0.02em;
}
.badge-rumor {
  display: inline-flex;
  align-items: center;
  gap: 4px;
  margin-left: auto;
  background: var(--gold-bg);
  color: var(--gold);
  border-radius: 999px;
  padding: 3px 9px 3px 8px;
  font-size: 11px;
  white-space: nowrap;
}
.badge-rumor .diamond { font-size: 9px; }
.badge-fix {
  background: #ece7dc;
  color: var(--ink-soft);
}
.badge-ok {
  background: var(--safe-bg);
  color: var(--safe-fg);
}

/* Market grid */
.market-grid {
  display: grid;
  grid-template-columns: 1fr 1fr;
  gap: 10px;
}
.commodity-card {
  background: var(--card);
  border: 1px solid var(--line);
  border-radius: 12px;
  padding: 12px 12px 10px;
  text-align: left;
  min-height: 108px;
  box-shadow: none;
  display: flex;
  flex-direction: column;
  gap: 2px;
  -webkit-tap-highlight-color: transparent;
}
.commodity-card:hover,
.commodity-card:focus-visible {
  border-color: var(--ink);
  outline: none;
}
.commodity-card:focus-visible {
  outline: 2px solid var(--ink);
  outline-offset: 2px;
}
.card-head {
  display: flex;
  align-items: center;
  gap: 8px;
  margin-bottom: 4px;
}
.icon-wrap {
  width: 28px;
  height: 28px;
  border-radius: 50%;
  display: inline-flex;
  align-items: center;
  justify-content: center;
  flex-shrink: 0;
}
.icon-wrap svg { width: 16px; height: 16px; }
.icon-wheat { background: #efe3c4; color: #8a6a2a; }
.icon-crude { background: #ece7e1; color: #2b2b2b; }
.icon-copper { background: #f3e0c2; color: #b56a1a; }
.icon-gold { background: #f6e7b0; color: #a6841d; }
.icon-shipping { background: #e4e7ec; color: #3a4554; }
.icon-coffee { background: #ead9cc; color: #6b3e2a; }
.card-name { font-size: 15px; font-weight: 700; }
.card-price-row {
  display: flex;
  align-items: baseline;
  justify-content: space-between;
  gap: 8px;
}
.card-price {
  font-size: 24px;
  font-weight: 700;
  letter-spacing: -0.03em;
  font-variant-numeric: tabular-nums;
  line-height: 1.15;
}
.card-chg {
  font-size: 13px;
  font-weight: 600;
  font-variant-numeric: tabular-nums;
}
.up { color: var(--green); }
.down { color: var(--red); }
.flat { color: var(--ink-faint); }
.card-pos {
  margin-top: 6px;
  font-size: 12px;
  color: var(--ink-soft);
  line-height: 1.45;
}
.card-pos .side-qty { color: var(--ink); }
.card-pos .pnl { font-weight: 600; }

.main-screen.active {
  display: flex;
  flex-direction: column;
  min-height: 100%;
}
.main-screen .section.market { flex: 1; padding-bottom: 12px; }

/* Sticky end-week bar */
.sticky-bar {
  position: sticky;
  bottom: 0;
  z-index: 8;
  margin-top: auto;
  padding: 14px 16px calc(12px + env(safe-area-inset-bottom, 0px));
  background: linear-gradient(180deg, rgba(244, 239, 227, 0), var(--paper) 40%);
  pointer-events: none;
}
.sticky-bar .btn-primary {
  margin-top: 0;
  letter-spacing: 0.22em;
  pointer-events: auto;
  position: relative;
  z-index: 9;
}

/* Overlays */
.overlay {
  display: none;
  position: fixed;
  inset: 0;
  z-index: 20;
  justify-content: center;
  align-items: flex-end;
  background: rgba(28, 25, 22, 0.45);
}
.overlay.open {
  display: flex !important;
}
.overlay[hidden] { display: none !important; }
.overlay.open:not([hidden]) { display: flex !important; }
.overlay .sheet,
.overlay .dialog {
  width: 100%;
  max-width: 430px;
  background: var(--card);
  max-height: 92vh;
  overflow: auto;
  -webkit-overflow-scrolling: touch;
}
.sheet {
  border-radius: 22px 22px 0 0;
  padding: 18px 20px calc(20px + env(safe-area-inset-bottom, 0px));
  box-shadow: 0 -8px 30px rgba(0, 0, 0, 0.15);
}
.dialog {
  border-radius: 16px 16px 0 0;
  padding: 22px 20px calc(22px + env(safe-area-inset-bottom, 0px));
}

.sheet-head {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 12px;
  margin-bottom: 16px;
}
.sheet-head .who {
  display: flex;
  align-items: center;
  gap: 8px;
  font-size: 22px;
  font-weight: 700;
}
.sheet-head .px {
  text-align: right;
}
.sheet-head .px .p {
  font-size: 22px;
  font-weight: 700;
  font-variant-numeric: tabular-nums;
}
.sheet-head .px .c { margin-left: 6px; font-size: 14px; font-weight: 600; }

.side-toggle {
  display: grid;
  grid-template-columns: 1fr 1fr;
  background: #f3eee4;
  border-radius: 8px;
  padding: 3px;
  margin-bottom: 14px;
}
.side-toggle button {
  border: 0;
  background: transparent;
  min-height: 42px;
  border-radius: 6px;
  font-size: 16px;
  font-weight: 600;
}
.side-toggle button[aria-pressed="true"] {
  background: var(--ink);
  color: var(--paper);
}

.amount-head {
  display: flex;
  justify-content: space-between;
  align-items: baseline;
  color: var(--ink-soft);
  font-size: 13px;
  margin-bottom: 4px;
}
.amount-head button {
  border: 0;
  background: none;
  color: var(--ink-soft);
  text-decoration: underline;
  text-underline-offset: 3px;
  min-height: 32px;
  font-size: 13px;
}
.amount-box {
  border: 1px solid var(--line);
  border-radius: 8px;
  padding: 8px 12px 10px;
  background: #fff;
  display: flex;
  align-items: baseline;
  gap: 8px;
}
.amount-box input {
  border: 0;
  outline: none;
  width: 100%;
  font-size: 40px;
  font-weight: 700;
  letter-spacing: -0.03em;
  background: transparent;
  font-variant-numeric: tabular-nums;
  min-height: 52px;
  padding: 0;
}
.amount-box .unit {
  white-space: nowrap;
  color: var(--ink-soft);
  font-size: 14px;
}
.amount-actions {
  display: flex;
  justify-content: flex-end;
  margin-top: 4px;
}
.amount-actions button {
  border: 0;
  background: none;
  color: var(--ink-soft);
  min-height: 36px;
  font-size: 13px;
  text-decoration: underline;
  text-underline-offset: 3px;
}

.preview-lines {
  margin: 10px 0 16px;
  font-size: 13px;
  color: var(--ink-soft);
  line-height: 1.7;
}
.preview-lines .warn { color: var(--danger-fg); }
.sheet .btn-primary { margin-top: 4px; }
.fee-hint {
  text-align: center;
  color: var(--ink-faint);
  font-size: 12px;
  margin: 10px 0 0;
}

/* Week settlement dialog */
.dialog h2 {
  text-align: center;
  font-size: 15px;
  letter-spacing: 0.2em;
  font-weight: 600;
  margin: 0 0 16px;
}
.settle-block {
  background: #fff;
  border: 1px solid var(--line);
  border-radius: 10px;
  padding: 12px 14px;
  margin-bottom: 10px;
}
.settle-block h3 {
  margin: 0 0 8px;
  font-size: 13px;
  letter-spacing: 0.08em;
  color: var(--ink-soft);
  font-weight: 600;
}
.settle-block ul {
  margin: 0;
  padding: 0;
  list-style: none;
}
.settle-block li {
  font-size: 13.5px;
  padding: 4px 0;
  border-top: 1px solid #f0e8d8;
}
.settle-block li:first-child { border-top: 0; }
.liq-alert {
  border-color: #e3b6ae;
  background: #fbf0ee;
}
.liq-alert h3 { color: var(--danger-fg); }

/* End screen */
.end-screen {
  padding: 18px 18px 36px;
  text-align: center;
}
.end-kicker {
  display: flex;
  align-items: center;
  justify-content: center;
  gap: 12px;
  margin: 18px 8px 6px;
  letter-spacing: 0.28em;
  font-size: 15px;
  font-weight: 600;
}
.end-kicker::before,
.end-kicker::after {
  content: "";
  flex: 1;
  height: 1px;
  background: var(--ink);
}
.end-label {
  color: var(--ink-soft);
  font-size: 13px;
  letter-spacing: 0.12em;
  margin-top: 10px;
}
.end-final {
  font-size: 56px;
  font-weight: 700;
  letter-spacing: -0.03em;
  line-height: 1.05;
  font-variant-numeric: tabular-nums;
  margin: 4px 0 8px;
}
.end-compare {
  font-size: 14px;
  color: var(--ink-soft);
  margin-bottom: 16px;
}
.end-compare .sep { margin: 0 8px; color: var(--line-strong); }
.stat-box {
  display: flex;
  justify-content: space-between;
  align-items: baseline;
  border: 1px solid var(--line);
  border-radius: 8px;
  padding: 14px 16px;
  margin: 0 4px 8px;
  background: #fffdf8;
  text-align: left;
  font-size: 15px;
}
.stat-box .right {
  font-size: 22px;
  font-weight: 700;
  font-variant-numeric: tabular-nums;
  text-align: right;
}
.stat-box .note {
  display: block;
  font-size: 12px;
  font-weight: 400;
  color: var(--ink-soft);
  margin-top: 2px;
}
.verdict {
  margin: 16px 4px 22px;
  padding: 8px 6px 4px;
  text-align: left;
  font-size: 14.5px;
  line-height: 1.75;
  color: var(--ink);
}
.verdict::before {
  content: "“";
  font-size: 36px;
  line-height: 0;
  color: var(--ink-faint);
  margin-right: 4px;
  vertical-align: -8px;
}
.end-early {
  color: var(--danger-fg);
  font-size: 13px;
  margin: 0 8px 8px;
}

/* Desktop breathing room */
@media (min-width: 720px) {
  body { background: #e4dccb; }
  #app { padding: 24px 0; align-items: flex-start; height: 100vh; height: 100dvh; }
  .shell {
    min-height: calc(100vh - 48px);
    min-height: calc(100dvh - 48px);
    height: calc(100vh - 48px);
    height: calc(100dvh - 48px);
    border-radius: 8px;
  }
  .screen { min-height: 100%; }
  .overlay { align-items: center; }
  .overlay .sheet,
  .overlay .dialog {
    border-radius: 16px;
    max-height: 86vh;
  }
}

@media (max-width: 389px) {
  .masthead h1, .masthead.framed h1 { font-size: 44px; }
  .status-panel .assets .value { font-size: 28px; }
  .main-screen .masthead h1 { font-size: 40px; }
  .end-final { font-size: 44px; }
  .card-price { font-size: 22px; }
}

/* Keep the language switcher clear of the framed title on start/end screens */
.start-screen .lang-switch,
.end-screen .lang-switch { top: 10px; }
.start-screen,
.end-screen { padding-top: 48px; }
.start-screen .masthead.framed,
.end-screen .masthead.framed { margin-top: 0; }
