:root {
    color-scheme: light;
    --bg: #f4f6f5; --surface: #fff; --ink: #17211b; --muted: #647067; --line: #d9dfdb;
    --green: #166534; --green-dark: #114d29; --green-soft: #e9f5ed; --charcoal: #26322b;
    --amber: #9a5b00; --amber-soft: #fff5dc; --red: #b42318; --red-soft: #fff0ee;
    --radius: 8px; --shadow: 0 8px 24px rgba(18, 39, 27, .08);
    font-family: Inter, "PingFang SC", "Microsoft YaHei", system-ui, sans-serif;
}

* { box-sizing: border-box; }
[hidden] { display: none !important; }
body { margin: 0; min-width: 320px; background: var(--bg); color: var(--ink); letter-spacing: 0; }
body.modal-open { overflow: hidden; }
button, input, select, textarea { font: inherit; letter-spacing: 0; }
button { cursor: pointer; }
button:disabled { cursor: not-allowed; opacity: .55; }
h1, h2, h3, p { margin-top: 0; }

.topbar { padding-top: env(safe-area-inset-top); background: var(--surface); border-bottom: 1px solid var(--line); }
.topbar-inner { max-width: 1040px; min-height: 96px; position: relative; margin: 0 auto; padding: 14px 20px; display: flex; align-items: center; justify-content: center; text-align: center; }
.brand-lockup { max-width: calc(100% - 76px); display: flex; align-items: center; justify-content: center; gap: 16px; }
.brand-logo { width: 64px; height: 64px; flex: 0 0 64px; object-fit: contain; }
.topbar-copy { min-width: 0; }
.topbar h1 { margin-bottom: 5px; font-size: 25px; line-height: 1.2; }
.topbar p { margin-bottom: 0; color: #465149; font-size: 15px; font-weight: 650; }
.header-logout { position: absolute; right: 20px; top: 50%; margin: 0; transform: translateY(-50%); }

.login-page { min-height: 100dvh; background: #eef2ef; }
.login-shell { min-height: 100dvh; max-width: none; margin: 0; padding: max(24px, env(safe-area-inset-top)) 18px max(24px, env(safe-area-inset-bottom)); display: grid; place-items: center; }
.login-panel { width: min(390px, 100%); padding: 34px 30px 30px; border: 1px solid var(--line); border-top: 4px solid var(--green); border-radius: var(--radius); background: var(--surface); box-shadow: var(--shadow); text-align: center; }
.login-logo { width: 82px; height: 82px; object-fit: contain; }
.login-panel h1 { margin: 16px 0 5px; font-size: 26px; }
.login-panel > p { margin-bottom: 26px; color: #465149; font-size: 15px; font-weight: 650; }
.login-form { margin: 0; display: grid; gap: 14px; text-align: left; }
.login-form .message { margin: 0; }
.login-form .button { margin-top: 2px; }

main { max-width: 1040px; margin: 0 auto; padding: 18px 20px 56px; }
.view-tabs { width: 100%; margin-bottom: 16px; padding: 4px; display: grid; grid-template-columns: 1fr 1fr; gap: 4px; border-radius: 8px; background: #e6ebe8; }
.view-tab { min-height: 42px; border: 0; border-radius: 6px; background: transparent; color: var(--muted); font-weight: 700; }
.view-tab.is-active { background: var(--surface); color: var(--ink); box-shadow: 0 1px 4px rgba(15, 30, 21, .12); }
.view { display: none; }
.view.is-active { display: block; }

.active-ledger { margin-bottom: 24px; padding: 12px 14px; display: flex; align-items: center; justify-content: space-between; gap: 16px; border-left: 4px solid var(--green); background: var(--surface); box-shadow: 0 2px 8px rgba(18, 39, 27, .05); }
.active-ledger div { min-width: 0; display: grid; gap: 2px; }
.active-ledger span { color: var(--green); font-size: 11px; font-weight: 800; }
.active-ledger strong, .active-ledger small { overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.active-ledger strong { font-size: 15px; }
.active-ledger small { color: var(--muted); font-size: 12px; }

.section-heading { margin-bottom: 18px; display: flex; align-items: flex-start; justify-content: space-between; gap: 16px; }
.eyebrow { margin-bottom: 5px; color: var(--green); font-size: 12px; font-weight: 800; }
.section-heading h2, .dialog-heading h2 { margin-bottom: 0; font-size: 24px; line-height: 1.3; }
.state-chip { flex: 0 0 auto; min-height: 28px; padding: 5px 10px; border: 1px solid var(--line); border-radius: 999px; background: var(--surface); color: var(--muted); font-size: 12px; font-weight: 700; }
.state-chip.is-success { color: var(--green); border-color: #a5d5b4; background: var(--green-soft); }
.state-chip.is-loading { color: #285b7a; border-color: #acd3ea; background: #eaf6fc; }
.state-chip.is-error { color: var(--red); border-color: #efb6b0; background: var(--red-soft); }

.scanner-tool { overflow: hidden; border-radius: var(--radius); background: var(--charcoal); box-shadow: var(--shadow); }
.scanner-placeholder { min-height: 290px; padding: 34px 20px 22px; display: flex; flex-direction: column; align-items: center; justify-content: center; color: #fff; text-align: center; }
.scanner-placeholder strong { font-size: 17px; }
.scanner-placeholder p { margin: 8px 0 0; color: #cbd5ce; font-size: 13px; }
.scan-frame { width: 160px; height: 160px; position: relative; margin-bottom: 22px; border: 2px solid rgba(255,255,255,.34); }
.scan-frame::before, .scan-frame::after, .scan-frame span::before, .scan-frame span::after { content: ""; position: absolute; width: 28px; height: 28px; border-color: #56c77a; border-style: solid; }
.scan-frame::before { left: -3px; top: -3px; border-width: 4px 0 0 4px; }
.scan-frame::after { right: -3px; top: -3px; border-width: 4px 4px 0 0; }
.scan-frame span::before { left: -3px; bottom: -3px; border-width: 0 0 4px 4px; }
.scan-frame span::after { right: -3px; bottom: -3px; border-width: 0 4px 4px 0; }
.reader { min-height: 300px; background: #111; }
.reader video { object-fit: cover; }
.scanner-actions { padding: 14px; display: grid; grid-template-columns: 1fr 1fr; gap: 10px; background: var(--surface); }

.button { min-height: 44px; padding: 10px 16px; border: 1px solid transparent; border-radius: 6px; font-weight: 750; line-height: 1.2; }
.button-small { min-height: 36px; padding: 7px 12px; font-size: 13px; }
.button-primary { border-color: var(--green); background: var(--green); color: #fff; }
.button-primary:hover { background: var(--green-dark); }
.button-secondary { border-color: var(--line); background: var(--surface); color: var(--ink); }
.button-dark { border-color: var(--charcoal); background: var(--charcoal); color: #fff; }
.button-danger { border-color: #efb6b0; background: var(--surface); color: var(--red); }
.button-danger:hover { border-color: var(--red); background: var(--red-soft); }

.manual-lookup { margin-top: 20px; }
.manual-lookup > label { display: block; margin-bottom: 7px; color: var(--muted); font-size: 13px; font-weight: 700; }
.input-action, .searchbar { display: grid; grid-template-columns: minmax(0, 1fr) auto; gap: 8px; }
input, select, textarea { width: 100%; min-height: 44px; padding: 10px 12px; border: 1px solid #cbd3ce; border-radius: 6px; outline: none; background: var(--surface); color: var(--ink); }
input:focus, select:focus, textarea:focus { border-color: var(--green); box-shadow: 0 0 0 3px rgba(22, 101, 52, .12); }
input[readonly], input:disabled { background: #edf0ee; color: #4e5a52; }
textarea { resize: vertical; }
.message { margin-top: 16px; padding: 12px 14px; border: 1px solid; border-radius: 6px; font-size: 14px; }
.message.is-success { color: var(--green); background: var(--green-soft); border-color: #a5d5b4; }
.message.is-error { color: var(--red); background: var(--red-soft); border-color: #efb6b0; }
.message.is-warning { color: var(--amber); background: var(--amber-soft); border-color: #e9c881; }

#device-form { margin-top: 24px; }
fieldset { margin: 0; padding: 24px 0; border: 0; border-top: 1px solid var(--line); }
legend { width: 100%; padding: 0 0 16px; font-size: 17px; font-weight: 800; }
.form-grid { display: grid; grid-template-columns: repeat(2, minmax(0, 1fr)); gap: 16px; }
.field { min-width: 0; display: flex; flex-direction: column; gap: 7px; color: #48534c; font-size: 13px; font-weight: 700; }
.field-wide { grid-column: 1 / -1; }
.pressure-range { display: grid; grid-template-columns: 1fr 1fr; gap: 10px; }
.option-with-custom { display: grid; gap: 7px; }
.option-with-custom select, .option-with-custom input { min-width: 0; }
.form-actions { position: sticky; z-index: 4; bottom: 0; padding: 12px 0 calc(12px + env(safe-area-inset-bottom)); display: grid; grid-template-columns: auto minmax(180px, 1fr); gap: 10px; border-top: 1px solid var(--line); background: rgba(244,246,245,.96); }

.ledger-heading { align-items: center; }
.searchbar { margin-bottom: 16px; }
.ledger-list { display: grid; gap: 10px; }
.ledger-item { overflow: hidden; border: 1px solid var(--line); border-radius: var(--radius); background: var(--surface); }
.ledger-summary { padding: 16px 18px; display: flex; align-items: center; justify-content: space-between; gap: 20px; }
.ledger-info { min-width: 0; }
.ledger-title-row { display: flex; align-items: center; gap: 9px; }
.ledger-title-row h3 { margin-bottom: 0; overflow-wrap: anywhere; font-size: 17px; line-height: 1.4; }
.count-badge { flex: 0 0 auto; padding: 3px 7px; border-radius: 999px; background: var(--green-soft); color: var(--green); font-size: 11px; font-weight: 800; }
.ledger-info p { margin: 4px 0; color: #39453d; font-size: 14px; }
.ledger-info small { color: var(--muted); font-size: 12px; }
.ledger-actions { flex: 0 0 auto; display: flex; gap: 8px; }
.ledger-details { overflow-x: auto; border-top: 1px solid var(--line); background: #fafbfa; }
.compact-device { min-width: 1040px; padding: 9px 14px; display: grid; grid-template-columns: 50px 80px 2fr 1.2fr 1.1fr 1.1fr 100px 132px; gap: 10px; border-bottom: 1px solid #e5e9e6; }
.compact-device:last-child { border-bottom: 0; }
.compact-device div { min-width: 0; }
.compact-device span { display: block; margin-bottom: 2px; color: var(--muted); font-size: 10px; }
.compact-device strong { display: block; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; font-size: 12px; font-weight: 600; }
.compact-actions { display: flex; align-items: center; justify-content: flex-end; gap: 9px; }
.text-action { padding: 3px 0; border: 0; background: transparent; color: var(--green); font-size: 12px; font-weight: 700; }
.text-action.is-danger { color: var(--red); }
.compact-empty { padding: 22px; color: var(--muted); text-align: center; font-size: 13px; }
.empty-state { padding: 64px 20px; border: 1px dashed #cbd3ce; border-radius: var(--radius); background: var(--surface); color: var(--muted); text-align: center; }

.modal-backdrop { position: fixed; z-index: 50; inset: 0; padding: 20px; display: grid; place-items: center; overflow-y: auto; background: rgba(13, 24, 17, .7); }
.ledger-dialog { width: min(900px, 100%); max-height: calc(100vh - 40px); overflow-y: auto; padding: 24px; border-radius: var(--radius); background: var(--surface); box-shadow: 0 20px 60px rgba(0,0,0,.24); }
.dialog-heading { margin-bottom: 20px; display: flex; justify-content: space-between; align-items: flex-start; }
.close-button { width: 40px; height: 40px; padding: 0; border: 0; background: transparent; color: var(--muted); font-size: 30px; line-height: 1; }
.dialog-columns { display: grid; grid-template-columns: 1.15fr .85fr; gap: 28px; }
.dialog-columns h3 { margin-bottom: 12px; font-size: 15px; }
.ledger-choice-panel { min-width: 0; }
.ledger-choices { max-height: 430px; overflow-y: auto; display: grid; gap: 8px; }
.ledger-choice { width: 100%; padding: 12px 14px; display: grid; gap: 3px; border: 1px solid var(--line); border-radius: 6px; background: #fafbfa; color: var(--ink); text-align: left; }
.ledger-choice:hover, .ledger-choice.is-selected { border-color: var(--green); background: var(--green-soft); }
.ledger-choice strong { overflow-wrap: anywhere; font-size: 14px; }
.ledger-choice span { color: #3f4b43; font-size: 13px; }
.ledger-choice small { color: var(--muted); font-size: 11px; }
.ledger-create-form { margin: 0; padding-left: 28px; display: grid; align-content: start; gap: 13px; border-left: 1px solid var(--line); }
.ledger-create-form .message { margin-top: 0; }
.loading-line { padding: 28px 10px; color: var(--muted); text-align: center; }
.loading-line.is-error { color: var(--red); }

.management-dialog { width: min(680px, 100%); max-height: calc(100vh - 40px); overflow-y: auto; padding: 24px; border-radius: var(--radius); background: var(--surface); box-shadow: 0 20px 60px rgba(0,0,0,.24); }
.device-dialog { width: min(820px, 100%); }
.management-form { margin: 0; }
.management-form fieldset:first-of-type { border-top: 0; padding-top: 0; }
.dialog-actions { margin-top: 22px; display: flex; align-items: center; justify-content: flex-end; gap: 10px; }
.device-dialog-actions { position: sticky; bottom: -24px; padding: 12px 0 calc(12px + env(safe-area-inset-bottom)); border-top: 1px solid var(--line); background: rgba(255,255,255,.96); }
.action-spacer { flex: 1 1 auto; }
.time-meta { margin: 0; color: var(--muted); font-size: 12px; }

@media (max-width: 700px) {
    main { padding: 14px 14px 42px; }
    .topbar-inner { min-height: 92px; padding: 10px 14px; }
    .brand-lockup { max-width: calc(100% - 82px); gap: 10px; }
    .brand-logo { width: 52px; height: 52px; flex-basis: 52px; }
    .topbar h1 { margin-bottom: 3px; font-size: 22px; }
    .topbar p { font-size: 13px; }
    .header-logout { right: 8px; }
    .header-logout .button { min-height: 34px; padding: 6px 9px; }
    .login-panel { padding: 28px 20px 24px; }
    .login-logo { width: 72px; height: 72px; }
    .login-panel h1 { font-size: 23px; }
    .login-panel > p { font-size: 14px; }
    .section-heading h2, .dialog-heading h2 { font-size: 21px; }
    .scanner-placeholder { min-height: 250px; }
    .scan-frame { width: 140px; height: 140px; }
    .scanner-actions { grid-template-columns: 1fr; }
    .input-action, .searchbar { grid-template-columns: minmax(0, 1fr) 92px; }
    .form-grid { grid-template-columns: 1fr; gap: 13px; }
    .pressure-range { grid-template-columns: 1fr; }
    .field-wide { grid-column: auto; }
    fieldset { padding: 22px 0; }
    .form-actions { grid-template-columns: 96px minmax(0, 1fr); }
    .active-ledger small { max-width: 220px; }
    .ledger-summary { padding: 14px; align-items: flex-start; }
    .ledger-actions { display: grid; grid-template-columns: 1fr 1fr; }
    .modal-backdrop { padding: 0; place-items: stretch; }
    .ledger-dialog { width: 100%; min-height: 100dvh; max-height: none; padding: 20px 14px calc(24px + env(safe-area-inset-bottom)); border-radius: 0; }
    .dialog-columns { grid-template-columns: 1fr; gap: 24px; }
    .ledger-create-form { padding: 22px 0 0; border-left: 0; border-top: 1px solid var(--line); }
    .ledger-choices { max-height: 250px; }
    .management-dialog { width: 100%; min-height: 100dvh; max-height: none; padding: 20px 14px calc(24px + env(safe-area-inset-bottom)); border-radius: 0; }
    .device-dialog-actions { bottom: calc(-24px - env(safe-area-inset-bottom)); display: grid; grid-template-columns: 1fr 1fr; }
    .device-dialog-actions .action-spacer { display: none; }
}

@media (prefers-reduced-motion: reduce) { * { scroll-behavior: auto !important; } }
