:root {
    --bg: #eef1f5;
    --surface: #ffffff;
    --surface-alt: #f7f9fc;
    --text: #18212f;
    --muted: #667085;
    --line: #d9e0ea;
    --line-strong: #c6d0dc;
    --primary: #23406a;
    --primary-soft: #eaf0f8;
    --success: #1f7a55;
    --success-soft: #e8f5ef;
    --danger: #b42318;
    --danger-soft: #fdecea;
    --shadow: 0 10px 28px rgba(15, 23, 42, 0.06);
}

* {
    box-sizing: border-box;
}

html, body {
    margin: 0;
    padding: 0;
    min-height: 100%;
    background: var(--bg);
    color: var(--text);
    font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, sans-serif;
}

body.app-body {
    display: flex;
    justify-content: center;
}

button, input, select, textarea {
    font: inherit;
    font-size: 16px;
}

a {
    color: inherit;
    text-decoration: none;
}

.app-shell {
    width: 100%;
    max-width: 430px;
    min-height: 100vh;
    background: var(--surface);
    box-shadow: 0 0 28px rgba(15, 23, 42, 0.08);
    position: relative;
}

.topbar {
    position: sticky;
    top: 0;
    z-index: 40;
    background: rgba(255, 255, 255, 0.97);
    backdrop-filter: blur(10px);
    border-bottom: 1px solid var(--line);
}

.topbar-inner {
    display: flex;
    align-items: center;
    justify-content: space-between;
    gap: 12px;
    min-height: 58px;
    padding: 10px 16px;
}

.topbar-title {
    display: flex;
    align-items: center;
    gap: 10px;
    min-width: 0;
    color: var(--primary);
    font-size: 18px;
    font-weight: 700;
    letter-spacing: -0.01em;
}

.topbar-title span:last-child {
    white-space: nowrap;
    overflow: hidden;
    text-overflow: ellipsis;
}

.topbar-back {
    display: inline-flex;
    align-items: center;
    justify-content: center;
    width: 26px;
    height: 26px;
    color: var(--primary);
    font-size: 24px;
    line-height: 1;
}

.topbar-chip {
    min-width: 34px;
    height: 34px;
    padding: 0 10px;
    border-radius: 10px;
    border: 1px solid var(--line);
    background: var(--surface-alt);
    color: var(--primary);
    display: inline-flex;
    align-items: center;
    justify-content: center;
    font-size: 12px;
    font-weight: 700;
}

.topbar-chip.is-empty {
    background: transparent;
    border-color: transparent;
}

.page-body {
    padding: 18px 16px 22px;
}

.stack {
    display: grid;
    gap: 12px;
}

.grid-2 {
    display: grid;
    grid-template-columns: 1fr 1fr;
    gap: 12px;
}

.grid-2.compact-header {
    grid-template-columns: minmax(0, 1.15fr) minmax(0, .85fr);
    gap: 10px;
}

.grid-2 > *,
.stack > *,
.row-between > *,
.field {
    min-width: 0;
}

.card {
    border: 1px solid var(--line);
    border-radius: 14px;
    background: var(--surface);
    box-shadow: var(--shadow);
}

.section-card {
    padding: 14px;
}

.field label {
    display: block;
    margin: 0 0 6px 2px;
    color: var(--muted);
    font-size: 11px;
    font-weight: 700;
    letter-spacing: 0.02em;
    text-transform: uppercase;
}

.field input,
.field select,
.field textarea,
.field-button,
.field-control {
    width: 100%;
    max-width: 100%;
    border: 1px solid var(--line-strong);
    border-radius: 10px;
    background: #fff;
    color: var(--text);
    transition: border-color .15s ease, box-shadow .15s ease;
}

.field input,
.field select,
.field-button,
.field-control {
    height: 42px;
    padding: 0 12px;
}

.field textarea {
    min-height: 110px;
    padding: 10px 12px;
    resize: vertical;
}

.field input:focus,
.field select:focus,
.field textarea:focus,
.field-button:focus,
.field-control:focus {
    outline: none;
    border-color: #98aac4;
    box-shadow: 0 0 0 3px rgba(35, 64, 106, 0.08);
}

.field-button {
    display: flex;
    align-items: center;
    justify-content: space-between;
    cursor: pointer;
    text-align: left;
}

.field.compact label {
    margin-bottom: 5px;
    font-size: 10px;
}

.field.compact input,
.field.compact select,
.field.compact .field-button,
.field.compact .field-control {
    height: 38px;
    padding: 0 10px;
    font-size: 14px;
}

input[type="date"] {
    min-width: 0;
    appearance: none;
    -webkit-appearance: none;
}

input[type="date"]::-webkit-date-and-time-value {
    min-width: 0;
    text-align: left;
}

.hint {
    color: var(--muted);
    font-size: 12px;
}

.muted {
    color: var(--muted);
    font-size: 13px;
}

.row-between {
    display: flex;
    align-items: center;
    justify-content: space-between;
    gap: 12px;
}

.page-title {
    margin: 0;
    font-size: 20px;
    line-height: 1.25;
    font-weight: 800;
    letter-spacing: -0.02em;
}

.page-subtitle {
    margin: 4px 0 0;
    color: var(--muted);
    font-size: 13px;
}

.stat-grid,
.nav-grid,
.list-stack {
    display: grid;
    gap: 12px;
}

.stat-grid {
    grid-template-columns: 1fr 1fr;
}

.stat-card,
.nav-card,
.list-card {
    padding: 14px;
}

.stat-label {
    color: var(--muted);
    font-size: 12px;
    font-weight: 700;
}

.stat-value {
    margin-top: 8px;
    font-size: 24px;
    font-weight: 800;
    letter-spacing: -0.03em;
}

.nav-card-title {
    font-size: 15px;
    font-weight: 800;
}

.nav-card-text {
    margin-top: 4px;
    color: var(--muted);
    font-size: 12px;
    line-height: 1.45;
}

.list-card {
    display: block;
}

.item-title {
    font-size: 16px;
    font-weight: 800;
    letter-spacing: -0.01em;
}

.item-subtitle {
    margin-top: 4px;
    color: var(--muted);
    font-size: 12px;
}

.item-meta {
    display: grid;
    grid-template-columns: 1fr 1fr;
    gap: 8px 16px;
    margin-top: 12px;
}

.meta-line {
    display: flex;
    justify-content: space-between;
    gap: 12px;
    font-size: 12px;
    color: var(--muted);
}

.meta-line strong {
    color: var(--text);
    font-weight: 700;
}

.badge {
    display: inline-flex;
    align-items: center;
    justify-content: center;
    min-height: 28px;
    padding: 0 10px;
    border-radius: 999px;
    font-size: 11px;
    font-weight: 700;
    white-space: nowrap;
}

.badge.waiting {
    background: var(--primary-soft);
    color: var(--primary);
}

.badge.done {
    background: var(--success-soft);
    color: var(--success);
}

.inline-actions {
    display: flex;
    gap: 8px;
    flex-wrap: wrap;
    margin-top: 12px;
}

.btn-inline {
    border: 0;
    border-radius: 10px;
    min-height: 36px;
    padding: 0 12px;
    font-size: 12px;
    font-weight: 700;
    cursor: pointer;
}

.btn-inline.primary {
    background: var(--primary-soft);
    color: var(--primary);
}

.btn-inline.danger {
    background: var(--danger-soft);
    color: var(--danger);
}

.unit-badges {
    display: flex;
    flex-wrap: wrap;
    gap: 6px;
    margin-top: 10px;
}

.unit-badge {
    display: inline-flex;
    align-items: center;
    justify-content: center;
    min-height: 26px;
    padding: 0 10px;
    border-radius: 999px;
    background: var(--surface-alt);
    color: #4e5d74;
    font-size: 11px;
    font-weight: 700;
}

.unit-badge.active {
    background: var(--primary-soft);
    color: var(--primary);
}

.danger-link {
    border: 0;
    background: none;
    color: var(--danger);
    font-size: 12px;
    font-weight: 700;
    padding: 0;
    cursor: pointer;
}

.section-tabs {
    display: flex;
    gap: 8px;
    padding: 0 16px 12px;
    overflow-x: auto;
    scrollbar-width: none;
}

.section-tabs::-webkit-scrollbar {
    display: none;
}

.tab-btn {
    border: 1px solid var(--line);
    border-radius: 10px;
    background: #fff;
    color: var(--muted);
    padding: 8px 12px;
    font-size: 12px;
    font-weight: 700;
    white-space: nowrap;
    cursor: pointer;
}

.tab-btn.active {
    border-color: #9eb0c8;
    background: var(--primary-soft);
    color: var(--primary);
}

.panel {
    display: none;
}

.panel.active {
    display: block;
}

.bottom-summary {
    background: rgba(255, 255, 255, 0.98);
    border-top: 1px solid var(--line);
    padding: 10px 16px;
}

.bottom-summary-header {
    display: flex;
    justify-content: space-between;
    gap: 12px;
    margin-bottom: 8px;
    font-size: 12px;
    font-weight: 700;
    color: var(--muted);
}

.bottom-summary-line {
    display: flex;
    justify-content: space-between;
    gap: 12px;
    padding-top: 8px;
    border-top: 1px solid var(--line);
    font-size: 13px;
}

.bottom-summary-line strong {
    font-size: 18px;
    font-weight: 800;
    letter-spacing: -0.02em;
}

.bottom-actions {
    display: grid;
    grid-template-columns: 1fr 1fr;
    gap: 10px;
    padding: 12px 16px max(12px, env(safe-area-inset-bottom));
    background: rgba(255, 255, 255, 0.98);
    border-top: 1px solid var(--line);
}

.btn {
    border: 0;
    border-radius: 10px;
    min-height: 44px;
    padding: 0 16px;
    font-size: 13px;
    font-weight: 700;
    cursor: pointer;
}

.btn-primary {
    background: var(--primary);
    color: #fff;
}

.btn-success {
    background: #dfe8f3;
    color: var(--primary);
}

.btn-soft {
    background: var(--surface-alt);
    color: var(--primary);
}

.floating-add {
    position: fixed;
    right: max(calc((100vw - 430px) / 2 + 20px), 20px);
    bottom: 24px;
    z-index: 35;
    width: 52px;
    height: 52px;
    border-radius: 14px;
    background: var(--primary);
    color: #fff;
    display: inline-flex;
    align-items: center;
    justify-content: center;
    font-size: 32px;
    box-shadow: 0 14px 28px rgba(35, 64, 106, 0.2);
}

.alert {
    margin: 12px 16px 0;
    padding: 12px 14px;
    border-radius: 12px;
    font-size: 13px;
    font-weight: 600;
}

.alert.error {
    background: var(--danger-soft);
    color: #7a271a;
}

.alert.success {
    background: var(--success-soft);
    color: #17543a;
}

.modal-backdrop {
    position: fixed;
    inset: 0;
    z-index: 90;
    background: rgba(15, 23, 42, 0.42);
    display: none;
    align-items: flex-end;
    justify-content: center;
}

.modal-backdrop.open {
    display: flex;
}

.modal-sheet {
    width: 100%;
    max-width: 430px;
    max-height: 82vh;
    overflow-y: auto;
    background: #fff;
    border-radius: 18px 18px 0 0;
    padding: 16px;
}

.modal-head {
    display: flex;
    align-items: center;
    justify-content: space-between;
    gap: 12px;
    margin-bottom: 14px;
}

.modal-title {
    font-size: 16px;
    font-weight: 800;
}

.modal-close {
    border: 0;
    background: none;
    font-size: 28px;
    color: var(--muted);
    padding: 0;
    line-height: 1;
    cursor: pointer;
}

.search-wrap {
    position: relative;
    margin-bottom: 12px;
}

.search-wrap input {
    width: 100%;
    height: 42px;
    border: 1px solid var(--line-strong);
    border-radius: 10px;
    background: var(--surface-alt);
    padding: 0 12px 0 36px;
}

.search-wrap span {
    position: absolute;
    left: 12px;
    top: 50%;
    transform: translateY(-50%);
    color: var(--muted);
}

.search-result {
    width: 100%;
    border: 1px solid var(--line);
    border-radius: 12px;
    padding: 12px;
    margin-bottom: 8px;
    background: #fff;
    text-align: left;
    cursor: pointer;
}

.search-result strong {
    display: block;
    font-size: 14px;
    font-weight: 700;
}

.search-result span {
    display: block;
    margin-top: 4px;
    color: var(--muted);
    font-size: 12px;
}

.empty-state {
    padding: 18px;
    border: 1px dashed var(--line-strong);
    border-radius: 12px;
    color: var(--muted);
    font-size: 13px;
    text-align: center;
    background: var(--surface-alt);
}

.doc-shell {
    display: flex;
    flex-direction: column;
    min-height: calc(100vh - 59px);
}

.doc-head {
    padding: 14px 16px 10px;
    display: grid;
    gap: 12px;
}

.doc-body {
    flex: 1;
    overflow: hidden;
}

.doc-scroll {
    height: calc(100vh - 360px);
    min-height: 220px;
    padding: 0 16px 10px;
    overflow-y: auto;
}

.line-card {
    padding: 12px;
    cursor: pointer;
}

.line-card.readonly {
    cursor: default;
}

.line-head {
    display: flex;
    align-items: flex-start;
    justify-content: space-between;
    gap: 12px;
}

.line-name {
    font-size: 15px;
    font-weight: 800;
    letter-spacing: -0.01em;
}

.line-code {
    margin-top: 3px;
    font-size: 11px;
    color: var(--muted);
}

.line-grid {
    display: grid;
    grid-template-columns: 1fr 1fr;
    gap: 8px 16px;
    margin-top: 12px;
}

.line-grid div {
    display: flex;
    justify-content: space-between;
    gap: 8px;
    color: var(--muted);
    font-size: 12px;
}

.line-grid strong {
    color: var(--text);
}

.doc-note {
    padding: 0 16px 10px;
}

.doc-summary {
    position: sticky;
    bottom: 69px;
    z-index: 20;
}

.doc-actions {
    position: sticky;
    bottom: 0;
    z-index: 25;
}

.toolbar {
    display: flex;
    justify-content: space-between;
    align-items: center;
    gap: 12px;
}

.toolbar-actions {
    display: flex;
    gap: 8px;
    flex-wrap: wrap;
}

.toolbar-title {
    min-width: 0;
}

.login-screen {
    min-height: 100vh;
    padding: 24px;
    display: flex;
    align-items: center;
    justify-content: center;
    background:
        radial-gradient(circle at top left, rgba(35, 64, 106, 0.07), transparent 34%),
        linear-gradient(180deg, #f4f6f9, #eef1f5);
}

.login-card {
    width: 100%;
    padding: 24px;
}

.login-title {
    margin: 0;
    font-size: 28px;
    font-weight: 800;
    color: var(--primary);
    letter-spacing: -0.03em;
}

.login-text {
    margin: 6px 0 20px;
    color: var(--muted);
    font-size: 13px;
}

.login-stack {
    display: grid;
    gap: 14px;
}

@media (max-width: 430px) {
    .grid-2,
    .grid-2.compact-header,
    .item-meta,
    .line-grid,
    .stat-grid {
        grid-template-columns: 1fr;
    }

    .bottom-actions {
        grid-template-columns: 1fr;
    }

    .doc-scroll {
        height: calc(100vh - 404px);
    }
}
