:root {
    --bg: #F3F4F2;
    --surface: #FFFFFF;
    --surface-2: #ECEFEC;
    --border: #D8DCD9;
    --text: #14171A;
    --text-dim: #5B646B;
    --accent: #1B8F5A;
    --accent-text: #FFFFFF;
    --accent-2: #C77800;
    --danger: #C4453C;
    --mono: 'IBM Plex Mono', ui-monospace, monospace;
    --sans: 'IBM Plex Sans', -apple-system, sans-serif;
    box-sizing: border-box;
    padding-top: env(safe-area-inset-top, 0px);
    padding-bottom: env(safe-area-inset-bottom, 0px);
  }

  @media (prefers-color-scheme: dark) {
    :root:not([data-theme="light"]) {
      --bg: #14171A;
      --surface: #1D2124;
      --surface-2: #242A2E;
      --border: #33393E;
      --text: #EDEFEE;
      --text-dim: #8A939B;
      --accent: #33D17E;
      --accent-text: #0B140F;
      --accent-2: #FFB020;
      --danger: #FF6B5D;
    }
  }

  :root[data-theme="dark"] {
    --bg: #14171A;
    --surface: #1D2124;
    --surface-2: #242A2E;
    --border: #33393E;
    --text: #EDEFEE;
    --text-dim: #8A939B;
    --accent: #33D17E;
    --accent-text: #0B140F;
    --accent-2: #FFB020;
    --danger: #FF6B5D;
  }

  html { scroll-padding-top: env(safe-area-inset-top, 0px); }
  * { box-sizing: border-box; }
  body {
    margin: 0;
    background: var(--bg);
    color: var(--text);
    font-family: var(--sans);
    -webkit-font-smoothing: antialiased;
    min-height: 100%;
  }

  .app {
    max-width: 560px;
    margin: 0 auto;
    padding: 20px 16px calc(28px + env(safe-area-inset-bottom, 0px));
    display: flex;
    flex-direction: column;
    gap: 18px;
  }

  header.top {
    display: flex;
    align-items: baseline;
    justify-content: space-between;
    padding-top: env(safe-area-inset-top, 0px);
  }
  .brand { display: flex; align-items: center; gap: 10px; }
  .brand-mark {
    width: 34px; height: 34px;
    border: 2px solid var(--text);
    border-radius: 7px;
    position: relative;
    flex-shrink: 0;
  }
  .brand-mark::before, .brand-mark::after {
    content: "";
    position: absolute;
    background: var(--accent);
  }
  .brand-mark::before { width: 10px; height: 10px; top: 5px; left: 5px; }
  .brand-mark::after { width: 4px; height: 4px; bottom: 6px; right: 6px; }
  h1 {
    font-size: 17px;
    font-weight: 700;
    letter-spacing: 0.01em;
    margin: 0;
    line-height: 1.1;
  }
  .subtitle { font-size: 11.5px; color: var(--text-dim); margin-top: 2px; font-family: var(--mono); }
  .theme-toggle {
    border: 1px solid var(--border);
    background: var(--surface);
    color: var(--text-dim);
    width: 34px; height: 34px;
    border-radius: 8px;
    display: flex; align-items: center; justify-content: center;
    cursor: pointer;
  }
  .theme-toggle svg { width: 16px; height: 16px; }

  /* Scanner panel */
  .scanner {
    background: var(--surface);
    border: 1px solid var(--border);
    border-radius: 14px;
    overflow: hidden;
  }
  .viewport {
    position: relative;
    aspect-ratio: 1 / 1;
    background: #0A0C0D;
    display: flex;
    align-items: center;
    justify-content: center;
    overflow: hidden;
  }
  video {
    width: 100%; height: 100%;
    object-fit: cover;
    display: none;
  }
  .viewport.active video { display: block; }

  .placeholder {
    display: flex;
    flex-direction: column;
    align-items: center;
    gap: 10px;
    color: #6E7A80;
    padding: 24px;
    text-align: center;
  }
  .viewport.active .placeholder { display: none; }
  .placeholder svg { width: 40px; height: 40px; opacity: 0.7; }
  .placeholder p { margin: 0; font-size: 13px; font-family: var(--mono); max-width: 220px; }

  .reticle {
    position: absolute;
    inset: 14%;
    display: none;
    pointer-events: none;
  }
  .viewport.active .reticle { display: block; }
  .corner {
    position: absolute;
    width: 26px; height: 26px;
    border: 3px solid var(--accent);
  }
  .corner.tl { top: 0; left: 0; border-right: none; border-bottom: none; border-radius: 6px 0 0 0; }
  .corner.tr { top: 0; right: 0; border-left: none; border-bottom: none; border-radius: 0 6px 0 0; }
  .corner.bl { bottom: 0; left: 0; border-right: none; border-top: none; border-radius: 0 0 0 6px; }
  .corner.br { bottom: 0; right: 0; border-left: none; border-top: none; border-radius: 0 0 6px 0; }
  .scanline {
    position: absolute;
    left: 4%; right: 4%;
    height: 2px;
    background: linear-gradient(90deg, transparent, var(--accent) 20%, var(--accent) 80%, transparent);
    box-shadow: 0 0 8px 1px var(--accent);
    animation: sweep 2.1s ease-in-out infinite;
  }
  @keyframes sweep {
    0%, 100% { top: 4%; opacity: 0.9; }
    50% { top: 94%; opacity: 0.5; }
  }
  @media (prefers-reduced-motion: reduce) {
    .scanline { animation: none; top: 50%; }
  }

  .viewport-status {
    position: absolute;
    top: 10px; left: 10px;
    font-family: var(--mono);
    font-size: 10.5px;
    letter-spacing: 0.04em;
    background: rgba(10,12,13,0.6);
    color: #C8D6CC;
    padding: 4px 8px;
    border-radius: 20px;
    display: none;
    align-items: center;
    gap: 5px;
  }
  .viewport.active .viewport-status { display: flex; }
  .dot { width: 6px; height: 6px; border-radius: 50%; background: var(--accent); animation: blink 1.4s infinite; }
  @keyframes blink { 50% { opacity: 0.25; } }

  .controls {
    display: grid;
    grid-template-columns: 1fr 1fr;
    gap: 1px;
    background: var(--border);
  }
  .ctrl-btn {
    background: var(--surface);
    border: none;
    color: var(--text);
    padding: 14px 10px;
    font-family: var(--sans);
    font-size: 13.5px;
    font-weight: 600;
    display: flex;
    align-items: center;
    justify-content: center;
    gap: 8px;
    cursor: pointer;
  }
  .ctrl-btn svg { width: 17px; height: 17px; flex-shrink: 0; }
  .ctrl-btn.on { color: var(--accent); }
  .ctrl-btn:active { background: var(--surface-2); }

  .hint {
    font-size: 12px;
    color: var(--text-dim);
    text-align: center;
    font-family: var(--mono);
    padding: 0 6px;
  }

  /* Result card */
  .result {
    display: none;
    background: var(--surface);
    border: 1px solid var(--accent);
    border-radius: 14px;
    padding: 16px;
    gap: 12px;
    flex-direction: column;
  }
  .result.show { display: flex; }
  .result-head {
    display: flex;
    justify-content: space-between;
    align-items: center;
  }
  .badge {
    font-family: var(--mono);
    font-size: 10.5px;
    letter-spacing: 0.05em;
    color: var(--accent);
    background: color-mix(in srgb, var(--accent) 14%, transparent);
    border: 1px solid var(--accent);
    padding: 3px 8px;
    border-radius: 20px;
  }
  .result-time { font-size: 11px; color: var(--text-dim); font-family: var(--mono); }
  .result-text {
    font-family: var(--mono);
    font-size: 14px;
    line-height: 1.5;
    word-break: break-word;
    background: var(--surface-2);
    border: 1px solid var(--border);
    border-radius: 8px;
    padding: 10px 12px;
    max-height: 140px;
    overflow-y: auto;
  }
  .result-actions { display: flex; gap: 8px; flex-wrap: wrap; }
  .action-btn {
    flex: 1;
    min-width: 90px;
    border: 1px solid var(--border);
    background: var(--surface-2);
    color: var(--text);
    padding: 9px 10px;
    border-radius: 8px;
    font-family: var(--sans);
    font-size: 12.5px;
    font-weight: 600;
    display: flex;
    align-items: center;
    justify-content: center;
    gap: 6px;
    cursor: pointer;
  }
  .action-btn svg { width: 14px; height: 14px; }
  .action-btn.primary { background: var(--accent); color: var(--accent-text); border-color: var(--accent); }

  /* History */
  .history-head {
    display: flex;
    align-items: center;
    justify-content: space-between;
    margin-top: 4px;
  }
  .history-head h2 {
    font-size: 12px;
    text-transform: none;
    letter-spacing: 0.02em;
    color: var(--text-dim);
    margin: 0;
    font-family: var(--mono);
  }
  .clear-btn {
    background: none;
    border: none;
    color: var(--danger);
    font-size: 12px;
    font-family: var(--mono);
    cursor: pointer;
    padding: 4px;
  }
  .history-list { display: flex; flex-direction: column; gap: 8px; }
  .history-empty {
    color: var(--text-dim);
    font-size: 13px;
    font-family: var(--mono);
    padding: 22px 10px;
    text-align: center;
    border: 1px dashed var(--border);
    border-radius: 10px;
  }
  .history-item {
    background: var(--surface);
    border: 1px solid var(--border);
    border-radius: 10px;
    padding: 11px 12px;
    display: flex;
    gap: 10px;
    align-items: flex-start;
  }
  .history-item .hi-format {
    font-family: var(--mono);
    font-size: 9.5px;
    color: var(--text-dim);
    border: 1px solid var(--border);
    border-radius: 5px;
    padding: 2px 5px;
    white-space: nowrap;
    margin-top: 2px;
  }
  .hi-body { flex: 1; min-width: 0; }
  .hi-text {
    font-family: var(--mono);
    font-size: 12.5px;
    white-space: nowrap;
    overflow: hidden;
    text-overflow: ellipsis;
  }
  .hi-time { font-size: 10.5px; color: var(--text-dim); margin-top: 2px; }
  .hi-actions { display: flex; gap: 4px; }
  .hi-icon-btn {
    background: none;
    border: none;
    color: var(--text-dim);
    width: 26px; height: 26px;
    display: flex; align-items: center; justify-content: center;
    cursor: pointer;
    border-radius: 6px;
  }
  .hi-icon-btn svg { width: 14px; height: 14px; }
  .hi-icon-btn:active { background: var(--surface-2); }

  input[type=file] { display: none; }

  .toast {
    position: fixed;
    bottom: calc(20px + env(safe-area-inset-bottom, 0px));
    left: 50%;
    transform: translateX(-50%) translateY(20px);
    background: var(--text);
    color: var(--bg);
    font-size: 12.5px;
    font-family: var(--sans);
    font-weight: 600;
    padding: 10px 16px;
    border-radius: 30px;
    opacity: 0;
    transition: opacity 0.2s ease, transform 0.2s ease;
    pointer-events: none;
    z-index: 50;
    white-space: nowrap;
    max-width: 88vw;
    overflow: hidden;
    text-overflow: ellipsis;
  }
  .toast.show { opacity: 1; transform: translateX(-50%) translateY(0); }
