/* ==============================
   色・サイズ
   ============================== */

:root {
    --bg: #f3f4f6;
    --surface: #ffffff;
    --surface-2: #f6f7f9;
    --text: #15181d;
    --muted: #6b7280;
    --border: #e3e6ea;
    --accent: #0e7a6d;
    --accent-strong: #0a5f55;
    --accent-soft: #e1f3f0;
    --on-accent: #ffffff;
    --amount: #0e7a6d;
    --danger: #b42318;
    --shadow: 0 1px 2px rgba(16, 24, 40, 0.06), 0 1px 3px rgba(16, 24, 40, 0.08);
    --bar-shadow: 0 -4px 16px rgba(16, 24, 40, 0.10);

    /* 硬貨・札の色 */
    --coin-alu: #d9dde2;
    --coin-alu-ink: #3b4148;
    --coin-brass: #d8b24a;
    --coin-brass-ink: #4a3808;
    --coin-copper: #b8683a;
    --coin-copper-ink: #ffffff;
    --coin-silver: #c3c9d0;
    --coin-silver-ink: #2b3036;
    --coin-gold: #c9a227;
    --coin-gold-ink: #3d3005;
    --bill-10000: #8a6d3b;
    --bill-5000: #7d5a8c;
    --bill-2000: #4f7a52;
    --bill-1000: #3f6b93;

    --radius: 14px;
    --tap: 48px;
    --header-h: 112px;
    --bar-h: 72px;
    --safe-bottom: env(safe-area-inset-bottom, 0px);

    color-scheme: light;
}

@media (prefers-color-scheme: dark) {
    :root:not([data-theme="light"]) {
        --bg: #111418;
        --surface: #1a1e24;
        --surface-2: #22272e;
        --text: #eceff3;
        --muted: #9aa3ad;
        --border: #2e343c;
        --accent: #3cc4ae;
        --accent-strong: #5fd6c2;
        --accent-soft: #173a35;
        --on-accent: #06201c;
        --amount: #5fd6c2;
        --danger: #f97066;
        --shadow: none;
        --bar-shadow: 0 -4px 16px rgba(0, 0, 0, 0.45);
        color-scheme: dark;
    }
}

:root[data-theme="dark"] {
    --bg: #111418;
    --surface: #1a1e24;
    --surface-2: #22272e;
    --text: #eceff3;
    --muted: #9aa3ad;
    --border: #2e343c;
    --accent: #3cc4ae;
    --accent-strong: #5fd6c2;
    --accent-soft: #173a35;
    --on-accent: #06201c;
    --amount: #5fd6c2;
    --danger: #f97066;
    --shadow: none;
    --bar-shadow: 0 -4px 16px rgba(0, 0, 0, 0.45);
    color-scheme: dark;
}


/* ==============================
   全体
   ============================== */

* {
    box-sizing: border-box;
}

html {
    scroll-behavior: smooth;
    -webkit-text-size-adjust: 100%;
    text-size-adjust: 100%;
}

body {
    margin: 0;
    padding-bottom: calc(var(--bar-h) + var(--safe-bottom) + 24px);
    font-family: -apple-system, BlinkMacSystemFont, "Hiragino Sans", "Hiragino Kaku Gothic ProN",
        "Noto Sans JP", "Yu Gothic UI", Meiryo, sans-serif;
    font-size: 16px;
    line-height: 1.5;
    color: var(--text);
    background: var(--bg);
    -webkit-tap-highlight-color: transparent;
}

button,
input,
a {
    font: inherit;
    color: inherit;
    touch-action: manipulation;
}

strong,
b,
.num-input,
.tab-sub,
.clock,
td {
    font-variant-numeric: tabular-nums;
}

:focus-visible {
    outline: 3px solid var(--accent);
    outline-offset: 2px;
}


/* ==============================
   ヘッダー
   ============================== */

.app-header {
    position: sticky;
    top: 0;
    z-index: 100;
    padding: calc(env(safe-area-inset-top, 0px) + 8px) 16px 8px;
    background: color-mix(in srgb, var(--bg) 88%, transparent);
    -webkit-backdrop-filter: blur(12px);
    backdrop-filter: blur(12px);
    border-bottom: 1px solid var(--border);
}

.header-top {
    display: flex;
    align-items: center;
    gap: 12px;
    max-width: 1100px;
    margin: 0 auto 8px;
}

.app-title {
    margin: 0;
    font-size: 18px;
    font-weight: 700;
    white-space: nowrap;
}

.clock {
    flex: 1;
    min-width: 0;
    font-size: 13px;
    color: var(--muted);
    white-space: nowrap;
    overflow: hidden;
    text-overflow: ellipsis;
}

.btn-ghost {
    min-height: 40px;
    padding: 0 14px;
    font-size: 14px;
    font-weight: 600;
    color: var(--danger);
    background: transparent;
    border: 1px solid var(--border);
    border-radius: 999px;
    cursor: pointer;
}

.btn-ghost:active {
    background: var(--surface-2);
}


/* タブ */

.tabs {
    display: grid;
    grid-template-columns: repeat(4, 1fr);
    gap: 6px;
    max-width: 1100px;
    margin: 0 auto;
}

.tab {
    display: flex;
    flex-direction: column;
    align-items: center;
    justify-content: center;
    min-height: 52px;
    padding: 4px 2px;
    text-decoration: none;
    background: var(--surface);
    border: 1px solid var(--border);
    border-radius: 12px;
    transition: background-color 0.15s, border-color 0.15s;
}

.tab-label {
    font-size: 14px;
    font-weight: 700;
    line-height: 1.2;
}

.tab-sub {
    max-width: 100%;
    font-size: 12px;
    color: var(--muted);
    line-height: 1.3;
    white-space: nowrap;
    overflow: hidden;
    text-overflow: ellipsis;
}

.tab.active {
    background: var(--accent-soft);
    border-color: var(--accent);
}

.tab.active .tab-sub {
    color: var(--accent-strong);
    font-weight: 600;
}


/* ==============================
   レイアウト・カード
   ============================== */

.layout {
    display: grid;
    gap: 16px;
    max-width: 1100px;
    margin: 0 auto;
    padding: 16px;
}

.card {
    container-type: inline-size;
    scroll-margin-top: calc(var(--header-h) + 12px);
    padding: 16px;
    background: var(--surface);
    border: 1px solid var(--border);
    border-radius: var(--radius);
    box-shadow: var(--shadow);
}

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

.card-head h2 {
    margin: 0;
    font-size: 20px;
}

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

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

.rows {
    display: grid;
    gap: 8px;
}

.rows[hidden] {
    display: none;
}


/* ==============================
   入力行
   スマホ：2段（金種＋入力 / 枚数＋金額）
   ============================== */

.row {
    display: grid;
    grid-template-columns: 1fr auto;
    grid-template-areas:
        "den    stepper"
        "meta   amount";
    align-items: center;
    column-gap: 12px;
    row-gap: 4px;
    padding: 10px 12px;
    background: var(--surface-2);
    border: 1px solid transparent;
    border-radius: 12px;
    transition: border-color 0.15s;
}

.row.has-value {
    border-color: color-mix(in srgb, var(--accent) 35%, transparent);
}

.row:focus-within {
    border-color: var(--accent);
}

.den {
    grid-area: den;
    display: flex;
    align-items: center;
    gap: 10px;
    min-width: 0;
    font-weight: 700;
    font-size: 17px;
    white-space: nowrap;
}

.den-text {
    font-size: 16px;
}

.meta {
    grid-area: meta;
    padding-left: 38px;
    font-size: 13px;
    color: var(--muted);
    white-space: nowrap;
}

.meta b {
    color: var(--text);
    font-size: 15px;
    margin-right: 1px;
}

.amount {
    grid-area: amount;
    justify-self: end;
    font-size: 14px;
    color: var(--muted);
    white-space: nowrap;
}

.amount b {
    font-size: 20px;
    color: var(--muted);
    margin-right: 2px;
}

.row.has-value .amount b {
    color: var(--amount);
}


/* 硬貨バッジ */

.coin-badge {
    display: inline-grid;
    place-items: center;
    flex: none;
    width: 28px;
    height: 28px;
    border-radius: 50%;
    box-shadow: inset 0 0 0 3px rgba(255, 255, 255, 0.35), inset 0 0 0 4px rgba(0, 0, 0, 0.08);
}

.coin-1 { background: var(--coin-alu); color: var(--coin-alu-ink); }
.coin-5 { background: var(--coin-brass); color: var(--coin-brass-ink); }
.coin-10 { background: var(--coin-copper); color: var(--coin-copper-ink); }
.coin-50 { background: var(--coin-silver); color: var(--coin-silver-ink); }
.coin-100 { background: var(--coin-silver); color: var(--coin-silver-ink); }
.coin-500 { background: var(--coin-gold); color: var(--coin-gold-ink); }

/* 札バッジ */

.bill-badge {
    display: inline-grid;
    place-items: center;
    flex: none;
    width: 34px;
    height: 20px;
    border-radius: 4px;
}

.bill-10000 { background: var(--bill-10000); }
.bill-5000 { background: var(--bill-5000); }
.bill-2000 { background: var(--bill-2000); }
.bill-1000 { background: var(--bill-1000); }

.bill-badge + .den-name,
.coin-badge + .den-name {
    font-size: 17px;
}


/* ステッパー（− 入力 ＋） */

.stepper {
    grid-area: stepper;
    display: flex;
    align-items: center;
    gap: 6px;
}

.step {
    display: grid;
    place-items: center;
    width: var(--tap);
    height: var(--tap);
    padding: 0;
    font-size: 24px;
    font-weight: 600;
    line-height: 1;
    color: var(--accent-strong);
    background: var(--surface);
    border: 1px solid var(--border);
    border-radius: 12px;
    cursor: pointer;
    user-select: none;
    -webkit-user-select: none;
    -webkit-touch-callout: none;
}

.step:active {
    background: var(--accent-soft);
    transform: scale(0.96);
}

.num-wrap {
    position: relative;
    display: block;
}

.num-input {
    display: block;
    width: 88px;
    height: var(--tap);
    padding: 0 28px 0 8px;
    /* 16px 以上で iPhone の自動ズームを防ぐ */
    font-size: 20px;
    font-weight: 700;
    text-align: right;
    background: var(--surface);
    border: 1px solid var(--border);
    border-radius: 12px;
    -webkit-appearance: none;
    appearance: none;
}

.num-input::placeholder {
    color: var(--muted);
    opacity: 0.6;
}

.num-input:focus {
    outline: none;
    border-color: var(--accent);
    box-shadow: 0 0 0 3px color-mix(in srgb, var(--accent) 25%, transparent);
}

.num-unit {
    position: absolute;
    right: 9px;
    top: 50%;
    transform: translateY(-50%);
    font-size: 13px;
    color: var(--muted);
    pointer-events: none;
}


/* 札（千円単位）入力 */

.row-bill-amount {
    grid-template-areas: "den stepper";
}

.thousand-field {
    grid-area: stepper;
    display: flex;
    align-items: center;
    gap: 4px;
}

.thousand-field .num-input {
    width: 128px;
    padding-right: 10px;
}

.thousand-suffix {
    font-size: 18px;
    font-weight: 700;
}


/* 札の入力方法切り替え */

.segmented {
    display: inline-flex;
    padding: 3px;
    background: var(--surface-2);
    border: 1px solid var(--border);
    border-radius: 999px;
}

.segmented button {
    min-width: 64px;
    min-height: 38px;
    padding: 0 14px;
    font-size: 14px;
    font-weight: 600;
    color: var(--muted);
    background: transparent;
    border: 0;
    border-radius: 999px;
    cursor: pointer;
}

.segmented button[aria-checked="true"] {
    color: var(--on-accent);
    background: var(--accent);
}


/* 小計 */

.subtotal {
    display: flex;
    align-items: baseline;
    justify-content: space-between;
    gap: 12px;
    margin-top: 12px;
    padding: 12px 4px 0;
    font-size: 15px;
    font-weight: 600;
    border-top: 1px dashed var(--border);
}

.subtotal strong {
    font-size: 24px;
    margin-right: 2px;
}


/* ==============================
   総合計
   ============================== */

.breakdown {
    margin: 0 0 12px;
}

.breakdown > div {
    display: flex;
    align-items: baseline;
    justify-content: space-between;
    padding: 10px 4px;
    border-bottom: 1px solid var(--border);
}

.breakdown dt {
    color: var(--muted);
    font-weight: 600;
}

.breakdown dd {
    margin: 0;
    font-size: 18px;
}

.breakdown dd span {
    font-weight: 700;
    margin-right: 2px;
    font-variant-numeric: tabular-nums;
}

.breakdown .grand {
    border-bottom: 0;
    padding-top: 14px;
}

.breakdown .grand dt {
    color: var(--text);
    font-size: 18px;
}

.breakdown .grand dd span {
    font-size: 32px;
    color: var(--amount);
}


/* 金種別内訳 */

.coin-detail {
    margin-bottom: 16px;
    background: var(--surface-2);
    border-radius: 12px;
}

.coin-detail summary {
    display: flex;
    align-items: center;
    min-height: var(--tap);
    padding: 0 14px;
    font-size: 15px;
    font-weight: 600;
    cursor: pointer;
    list-style: none;
}

.coin-detail summary::-webkit-details-marker {
    display: none;
}

.coin-detail summary::after {
    content: "";
    width: 8px;
    height: 8px;
    margin-left: auto;
    border-right: 2px solid var(--muted);
    border-bottom: 2px solid var(--muted);
    transform: rotate(45deg);
    transition: transform 0.2s;
}

.coin-detail[open] summary::after {
    transform: rotate(-135deg);
}

.table-wrap {
    overflow-x: auto;
    padding: 0 8px 10px;
}

.coin-detail table {
    width: 100%;
    border-collapse: collapse;
    font-size: 14px;
    white-space: nowrap;
}

.coin-detail th,
.coin-detail td {
    padding: 8px 6px;
    text-align: right;
    border-bottom: 1px solid var(--border);
}

.coin-detail thead th {
    font-size: 12px;
    font-weight: 600;
    color: var(--muted);
}

.coin-detail th:first-child {
    text-align: left;
}

.coin-detail tbody tr:last-child > * {
    border-bottom: 0;
}

.coin-detail td[data-col="amount"] {
    font-weight: 700;
}


/* ==============================
   計算式（計算シートの形式）
   ============================== */

.sheet {
    margin-bottom: 16px;
    font-variant-numeric: tabular-nums;
}

.sheet-head {
    display: flex;
    flex-wrap: wrap;
    justify-content: space-between;
    gap: 4px 16px;
    padding: 0 4px 8px;
    font-weight: 700;
}

.sheet-summary {
    margin: 0 0 8px;
}

.sheet-summary > div {
    display: flex;
    align-items: baseline;
    justify-content: space-between;
    padding: 6px 4px;
    border-bottom: 1px solid var(--border);
}

.sheet-summary dt {
    font-weight: 600;
}

.sheet-summary dd {
    margin: 0;
}

.sheet-summary b {
    font-size: 17px;
    margin-right: 2px;
}

.sheet-summary .grand b {
    font-size: 20px;
    color: var(--amount);
}

.sheet h3 {
    margin: 16px 4px 4px;
    font-size: 16px;
}

.sheet .table-wrap {
    padding: 0;
}

.formula {
    width: 100%;
    border-collapse: collapse;
    font-size: 14px;
    white-space: nowrap;
}

.formula th,
.formula td {
    padding: 6px 4px;
    text-align: right;
    border-bottom: 1px solid var(--border);
}

.formula tbody th {
    font-weight: 700;
}

.formula b {
    font-size: 15px;
    margin-right: 1px;
}

.formula .op {
    color: var(--muted);
}

.formula .result b {
    color: var(--amount);
}

.formula tr.zero td,
.formula tr.zero th {
    color: var(--muted);
    opacity: 0.55;
}

.formula tr.zero .result b {
    color: inherit;
}

.formula tfoot th,
.formula tfoot td {
    border-bottom: 0;
    font-weight: 700;
}

.formula tfoot .result b {
    font-size: 18px;
}


/* ボタン */

.btn-primary {
    display: inline-flex;
    align-items: center;
    justify-content: center;
    width: 100%;
    min-height: 52px;
    padding: 0 20px;
    font-size: 16px;
    font-weight: 700;
    color: var(--on-accent);
    text-decoration: none;
    background: var(--accent);
    border: 0;
    border-radius: 12px;
    cursor: pointer;
}

.btn-primary:active {
    background: var(--accent-strong);
    transform: scale(0.98);
}

.btn-small {
    width: auto;
    min-height: 44px;
    padding: 0 18px;
    font-size: 15px;
}


/* ==============================
   下部固定の総合計バー
   ============================== */

.total-bar {
    position: fixed;
    left: 0;
    right: 0;
    bottom: 0;
    z-index: 100;
    padding: 10px 16px calc(10px + var(--safe-bottom));
    background: var(--surface);
    border-top: 1px solid var(--border);
    box-shadow: var(--bar-shadow);
}

.total-bar-inner {
    display: flex;
    align-items: center;
    justify-content: space-between;
    gap: 12px;
    max-width: 1100px;
    margin: 0 auto;
}

.total-bar-text {
    display: flex;
    flex-direction: column;
    min-width: 0;
    line-height: 1.2;
}

.total-bar-label {
    font-size: 12px;
    font-weight: 600;
    color: var(--muted);
}

.total-bar-value {
    font-size: 15px;
    font-weight: 600;
    white-space: nowrap;
}

.total-bar-value strong {
    font-size: 28px;
    color: var(--amount);
    margin-right: 2px;
}


/* トースト */

.toast {
    position: fixed;
    left: 50%;
    bottom: calc(var(--bar-h) + var(--safe-bottom) + 16px);
    z-index: 200;
    padding: 10px 18px;
    font-size: 14px;
    font-weight: 600;
    color: var(--bg);
    background: var(--text);
    border-radius: 999px;
    opacity: 0;
    transform: translate(-50%, 8px);
    transition: opacity 0.2s, transform 0.2s;
    pointer-events: none;
}

.toast.show {
    opacity: 1;
    transform: translate(-50%, 0);
}


/* ==============================
   カード幅が広いとき：1行で表示
   ============================== */

@container (min-width: 520px) {
    .row {
        grid-template-columns: minmax(7.5rem, auto) 1fr auto minmax(8.5rem, auto);
        grid-template-areas: "den meta stepper amount";
        column-gap: 14px;
    }

    .row-bill-amount {
        grid-template-columns: 1fr auto;
        grid-template-areas: "den stepper";
    }

    .meta {
        padding-left: 0;
        text-align: right;
    }
}


/* ==============================
   カード幅が狭いとき（一般的なスマホ）
   ============================== */

@container (max-width: 359px) {
    .row {
        column-gap: 8px;
    }

    .stepper {
        gap: 4px;
    }

    .step {
        width: 44px;
    }

    .num-input {
        width: 76px;
    }

    .bill-badge {
        display: none;
    }

    .row[data-key^="billCount"] .meta {
        padding-left: 0;
    }
}

/* さらに狭いとき（幅 320px 前後の端末） */
@container (max-width: 300px) {
    .coin-badge {
        display: none;
    }

    .meta {
        padding-left: 0;
    }

    .row {
        padding: 8px 10px;
    }
}

@media (max-width: 400px) {
    .clock-year {
        display: none;
    }
}

@media (max-width: 359px) {
    .layout {
        padding: 12px;
        gap: 12px;
    }

    .card {
        padding: 12px;
    }

    .clock {
        display: none;
    }
}


/* ==============================
   タブレット・PC：2列
   ============================== */

@media (min-width: 900px) {
    .layout {
        grid-template-columns: 1fr 1fr;
        align-items: start;
        gap: 20px;
        padding: 20px;
    }

    .card-sheet {
        grid-column: 1 / -1;
    }

    .app-title {
        font-size: 20px;
    }

    .clock {
        font-size: 14px;
    }

    .tab {
        flex-direction: row;
        gap: 8px;
        min-height: 46px;
    }

    .tab-sub {
        font-size: 13px;
    }
}

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

    *,
    *::before,
    *::after {
        transition: none !important;
    }
}
