/* ============================================================
   かんたん家計簿Web ― デザイントークン
   紙の家計簿ノートを電子化したような、温かみのある「台帳」の質感。
   ============================================================ */

@import url('https://fonts.googleapis.com/css2?family=Shippori+Mincho:wght@500;700&family=Noto+Sans+JP:wght@400;500;700&display=swap');

:root {
    --paper:        #F6F1E7;
    --paper-line:   #E4DAC2;
    --ink:          #2B3A4A;
    --ink-soft:     #5B6B7A;
    --hanko:        #C0463C;   /* 支出・強調(判子の朱色) */
    --hanko-dark:   #9C332B;
    --ledger-green: #3E7C59;   /* 収入 */
    --gold:         #B4924C;
    --card-bg:      #FFFDF8;
    --shadow:       0 2px 10px rgba(43, 58, 74, 0.08);
    --radius:       14px;
    --font-display: 'Shippori Mincho', serif;
    --font-body:    'Noto Sans JP', -apple-system, BlinkMacSystemFont, 'Hiragino Sans', sans-serif;
}

* { box-sizing: border-box; }
img, canvas, svg { max-width: 100%; }

html, body {
    margin: 0;
    padding: 0;
    background: var(--paper);
    color: var(--ink);
    font-family: var(--font-body);
    font-size: 15px;
    line-height: 1.7;
}

body {
    background-image:
        linear-gradient(var(--paper-line) 1px, transparent 1px);
    background-size: 100% 2.4em;
    background-attachment: local;
}

a { color: var(--ledger-green); text-decoration: none; }
a:hover { text-decoration: underline; }

h1, h2, h3 { font-family: var(--font-display); font-weight: 700; margin: 0 0 .5em; color: var(--ink); }

.amount { font-variant-numeric: tabular-nums; font-weight: 700; }
.amount.income  { color: var(--ledger-green); }
.amount.expense { color: var(--hanko-dark); }

/* ---------- レイアウト共通 ---------- */
.page-shell {
    max-width: 480px;
    margin: 0 auto;
    min-height: 100vh;
    background: var(--paper);
    position: relative;
    padding-bottom: calc(84px + env(safe-area-inset-bottom, 0));
    overflow-x: hidden;
}

/* ノートの綴じ穴（署名要素） */
.spine {
    position: fixed;
    left: 0; top: 0; bottom: 0;
    width: 22px;
    background: linear-gradient(90deg, #EADFC2, #F6F1E7);
    display: flex;
    flex-direction: column;
    align-items: center;
    justify-content: flex-start;
    gap: 28px;
    padding-top: 40px;
    z-index: 1;
}
.spine span {
    width: 9px; height: 9px; border-radius: 50%;
    background: var(--paper);
    box-shadow: inset 0 1px 2px rgba(0,0,0,.25);
}
@media (max-width: 520px) { .spine { display: none; } }

.topbar {
    position: sticky; top: 0; z-index: 5;
    display: flex; align-items: center; justify-content: space-between;
    padding: 16px 20px;
    background: rgba(246,241,231,.92);
    backdrop-filter: blur(4px);
    border-bottom: 1px solid var(--paper-line);
}
.topbar h1 { font-size: 20px; margin: 0; }
.topbar .stamp-badge {
    width: 34px; height: 34px; border-radius: 50%;
    border: 2px solid var(--hanko);
    color: var(--hanko);
    display: flex; align-items: center; justify-content: center;
    font-family: var(--font-display); font-weight: 700; font-size: 13px;
    transform: rotate(-8deg);
}

.container { padding: 20px; }

.card {
    background: var(--card-bg);
    border: 1px solid var(--paper-line);
    border-radius: var(--radius);
    box-shadow: var(--shadow);
    padding: 20px;
    margin-bottom: 16px;
    max-width: 100%;
}

/* 狭い画面（iPhone SEなど）では余白を詰めてカレンダーなどがはみ出さないようにする */
@media (max-width: 360px) {
    .container { padding: 12px; }
    .card { padding: 14px; }
    .calendar-grid { gap: 2px; }
}

/* ---------- フォーム ---------- */
label { display: block; font-size: 13px; color: var(--ink-soft); margin: 14px 0 6px; }
input[type=text], input[type=email], input[type=password], input[type=number],
input[type=date], input[type=file], input[type=color], select, textarea {
    width: 100%;
    max-width: 100%;
    padding: 12px 14px;
    font-size: 16px; /* iOSでフォーカス時に自動ズームされるのを防ぐため16px以上にする */
    font-family: var(--font-body);
    border: 1px solid var(--paper-line);
    border-radius: 10px;
    background: #fff;
    color: var(--ink);
    box-sizing: border-box;
    -webkit-appearance: none;
    appearance: none;
}
/* input[type=date] は appearance:auto にすると一部のモバイルSafariで
   box-sizing:border-box が無視されて右にはみ出す不具合があるため、
   他の入力欄と同じ appearance:none のスタイルで統一する
   （appearance:none にしてもタップすればネイティブの日付ピッカーは開く） */
input[type=file] {
    padding: 10px 8px;
    background: var(--paper);
}
input[type=color] {
    -webkit-appearance: auto;
    appearance: auto;
    width: 64px;
    max-width: 64px;
    padding: 4px;
    height: 46px;
}
select {
    -webkit-appearance: none;
    appearance: none;
    background-image: url("data:image/svg+xml;utf8,<svg xmlns='http://www.w3.org/2000/svg' width='12' height='8'><path d='M1 1l5 5 5-5' stroke='%235B6B7A' stroke-width='1.6' fill='none' stroke-linecap='round' stroke-linejoin='round'/></svg>");
    background-repeat: no-repeat;
    background-position: right 14px center;
    padding-right: 34px;
}
input:focus, select:focus, textarea:focus {
    outline: none;
    border-color: var(--gold);
    box-shadow: 0 0 0 3px rgba(180,146,76,.2);
}

.btn {
    display: inline-flex; align-items: center; justify-content: center; gap: 8px;
    width: 100%;
    padding: 13px 18px;
    margin-top: 20px;
    border: none;
    border-radius: 999px;
    font-size: 15px;
    font-weight: 700;
    font-family: var(--font-body);
    cursor: pointer;
    transition: transform .1s ease, box-shadow .15s ease;
}
.btn:active { transform: scale(.98); }
.btn-primary { background: var(--hanko); color: #fff; box-shadow: 0 4px 12px rgba(192,70,60,.35); }
.btn-secondary { background: transparent; color: var(--ink); border: 1px solid var(--paper-line); }
.btn-ghost { background: transparent; color: var(--ink-soft); box-shadow: none; margin-top: 8px; }
.btn-block-small { width: auto; padding: 8px 16px; margin-top: 0; font-size: 13px; }

.type-toggle { display: flex; gap: 8px; margin-top: 14px; }
.type-toggle label {
    flex: 1; margin: 0; text-align: center; padding: 10px; border-radius: 10px;
    border: 1px solid var(--paper-line); cursor: pointer; color: var(--ink-soft); font-size: 14px;
}
.type-toggle input { display: none; }
.type-toggle input:checked + span { font-weight: 700; }
.type-toggle .opt-expense:has(input:checked) { background: rgba(192,70,60,.12); border-color: var(--hanko); color: var(--hanko-dark); }
.type-toggle .opt-income:has(input:checked) { background: rgba(62,124,89,.12); border-color: var(--ledger-green); color: var(--ledger-green); }

/* ---------- アラート / フラッシュ ---------- */
.alert { padding: 12px 16px; border-radius: 10px; margin-bottom: 16px; font-size: 14px; }
.alert-success { background: rgba(62,124,89,.12); color: var(--ledger-green); }
.alert-error   { background: rgba(192,70,60,.12); color: var(--hanko-dark); }
.alert-info    { background: rgba(90,155,224,.14); color: #2f5f8a; }

.calendar-grid.cal-font-small .day { font-size: 9px; }
.calendar-grid.cal-font-small .day .num { font-size: 10px; }
.calendar-grid.cal-font-large .day { font-size: 13px; }
.calendar-grid.cal-font-large .day .num { font-size: 15px; }

/* ---------- カレンダー ---------- */
.month-nav { display: flex; align-items: center; justify-content: space-between; margin-bottom: 12px; }
.month-nav .month-label { font-family: var(--font-display); font-size: 19px; }
.month-nav a { color: var(--ink); font-size: 20px; padding: 4px 10px; }

.summary-row { display: flex; gap: 10px; margin-bottom: 16px; }
.summary-box { flex: 1; text-align: center; padding: 12px 8px; border-radius: 12px; background: var(--card-bg); border: 1px solid var(--paper-line); }
.summary-box .label { font-size: 12px; color: var(--ink-soft); }
.summary-box .value { font-size: 17px; margin-top: 4px; }

.calendar-grid {
    display: grid;
    grid-template-columns: repeat(7, minmax(0, 1fr)); /* 1frのみだと中身の最小幅で列が広がりはみ出すため minmax(0, 1fr) にする */
    gap: 3px;
    width: 100%;
}
.calendar-grid .dow { text-align: center; font-size: 12px; color: var(--ink-soft); padding-bottom: 4px; min-width: 0; }
.calendar-grid .day {
    aspect-ratio: 1 / 1.05;
    border-radius: 8px;
    background: var(--card-bg);
    min-width: 0;
    overflow: hidden;
    border: 1px solid var(--paper-line);
    padding: 4px 3px;
    font-size: 11px;
    display: flex; flex-direction: column; gap: 2px;
    color: var(--ink);
    position: relative;
}
.calendar-grid .day.empty { background: transparent; border: none; }
.calendar-grid .day.today { border: 2px solid var(--gold); }
.calendar-grid .day .num { font-weight: 700; font-size: 12px; }
.calendar-grid .day .d-expense { color: var(--hanko-dark); font-size: 10px; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.calendar-grid .day .d-income  { color: var(--ledger-green); font-size: 10px; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.calendar-grid .day a { color: inherit; text-decoration: none; display: block; height: 100%; }
.calendar-grid .day.no-record::after {
    content: '🌱';
    position: absolute; right: 2px; top: 2px; font-size: 9px; opacity: .6;
}

/* ---------- リスト ---------- */
.tx-list { list-style: none; margin: 0; padding: 0; }
.tx-item {
    display: flex; flex-direction: column; gap: 8px;
    padding: 14px 0;
    border-bottom: 1px dashed var(--paper-line);
}
.tx-item:last-child { border-bottom: none; }
.tx-row-main { display: flex; align-items: center; gap: 12px; min-width: 0; }
.tx-icon { width: 38px; height: 38px; border-radius: 50%; background: var(--paper); display: flex; align-items: center; justify-content: center; font-size: 18px; flex-shrink: 0; }
.tx-main { flex: 1; min-width: 0; }
.tx-cat { font-size: 13px; color: var(--ink-soft); }
.tx-cat-link {
    background: none; border: none; padding: 0; margin: 0; cursor: pointer;
    font-size: 13px; color: var(--ink-soft); text-decoration: underline; text-decoration-style: dotted;
    display: inline-flex; align-items: center; gap: 4px; font-family: var(--font-body);
}
.receipt-badge { font-size: 12px; }
.tx-memo { font-size: 14px; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.tx-thumb { width: 38px; height: 38px; border-radius: 8px; object-fit: cover; border: 1px solid var(--paper-line); }
.tx-actions { display: flex; gap: 8px; justify-content: flex-end; padding-left: 50px; }
.tx-btn {
    display: inline-flex; align-items: center; gap: 4px;
    font-size: 13px; font-weight: 600;
    padding: 7px 14px;
    border-radius: 999px;
    border: 1px solid var(--paper-line);
    background: var(--paper);
    color: var(--ink);
    text-decoration: none;
    cursor: pointer;
    min-height: 32px;
}
.tx-btn-edit { color: var(--ink); }
.tx-btn-delete { color: var(--hanko-dark); border-color: rgba(192,70,60,.35); background: rgba(192,70,60,.08); }
.tx-btn:active { opacity: .7; }

/* ---------- レシート拡大表示（ライトボックス） ---------- */
.lightbox {
    display: none;
    position: fixed; inset: 0; z-index: 200;
    background: rgba(20, 16, 10, .85);
    align-items: center; justify-content: center;
    padding: 24px;
}
.lightbox.show { display: flex; }
.lightbox img {
    max-width: 100%; max-height: 100%;
    border-radius: 10px;
    box-shadow: 0 10px 40px rgba(0,0,0,.4);
}

/* ---------- タブバー(下部固定ナビ) ---------- */
.tabbar {
    position: fixed; left: 50%; transform: translateX(-50%);
    bottom: 0; width: 100%; max-width: 480px;
    display: flex;
    background: var(--card-bg);
    border-top: 1px solid var(--paper-line);
    z-index: 10;
    padding-bottom: env(safe-area-inset-bottom, 0);
}
.tabbar a {
    flex: 1; text-align: center; padding: 12px 2px 10px;
    color: var(--ink-soft); font-size: 11px; text-decoration: none;
    min-width: 0;
}
.tabbar a.active { color: var(--hanko); font-weight: 700; }
.tabbar .icon { display: block; font-size: 19px; margin-bottom: 2px; }

/* ---------- 判子スタンプ(署名要素: 保存完了の演出) ---------- */
.hanko-stamp {
    position: fixed; top: 50%; left: 50%;
    transform: translate(-50%, -50%) rotate(-12deg) scale(0);
    width: 120px; height: 120px; border-radius: 50%;
    border: 5px solid var(--hanko);
    color: var(--hanko);
    display: flex; align-items: center; justify-content: center;
    font-family: var(--font-display); font-size: 26px; font-weight: 700;
    background: rgba(255,255,255,.9);
    z-index: 100;
    pointer-events: none;
    opacity: 0;
}
.hanko-stamp.show {
    animation: stamp-in .5s cubic-bezier(.2,1.4,.4,1) forwards;
}
@keyframes stamp-in {
    0%   { transform: translate(-50%, -50%) rotate(-12deg) scale(2.4); opacity: 0; }
    60%  { transform: translate(-50%, -50%) rotate(-8deg) scale(.95); opacity: 1; }
    100% { transform: translate(-50%, -50%) rotate(-8deg) scale(1); opacity: 0; animation-delay: .5s; }
}

/* ---------- カテゴリー管理 ---------- */
.cat-row { display: flex; align-items: center; gap: 10px; padding: 10px 0; border-bottom: 1px dashed var(--paper-line); }
.cat-row .swatch { width: 28px; height: 28px; border-radius: 50%; display: flex; align-items: center; justify-content: center; font-size: 14px; flex-shrink: 0; }
.cat-row .name { flex: 1; }
.cat-row .drag { color: var(--ink-soft); cursor: grab; }
.cat-row button:disabled { opacity: .3; cursor: default; }

/* ---------- 認証画面(register/login) ---------- */
.auth-shell {
    max-width: 400px; margin: 0 auto; padding: 48px 20px;
    min-height: 100vh; display: flex; flex-direction: column; justify-content: center;
}
.auth-logo { text-align: center; margin-bottom: 28px; }
.auth-logo .stamp-badge { margin: 0 auto 10px; width: 56px; height: 56px; font-size: 22px; }
.auth-logo h1 { font-size: 24px; }
.auth-switch { text-align: center; margin-top: 18px; font-size: 14px; color: var(--ink-soft); }

.empty-state { text-align: center; color: var(--ink-soft); padding: 40px 10px; }
.empty-state .em-icon { font-size: 34px; display: block; margin-bottom: 8px; }

.link-row { display:flex; justify-content: space-between; font-size: 13px; margin-top: 6px; }

/* ---------- 設定メニュー ---------- */
.settings-group-title { font-size: 12px; color: var(--ink-soft); margin: 20px 4px 8px; }
.settings-row {
    display: flex; align-items: center; justify-content: space-between;
    padding: 14px 16px;
    text-decoration: none; color: var(--ink);
    border-bottom: 1px solid var(--paper-line);
    font-size: 15px;
}
.settings-row:last-child { border-bottom: none; }
.settings-row:hover { background: rgba(0,0,0,.02); }
.settings-row-label { font-weight: 500; }
.settings-row-value { color: var(--ink-soft); font-size: 13px; display:flex; align-items:center; gap:4px; }
.settings-row-value .chev { color: var(--ink-soft); font-size: 16px; }
.color-swatch { display:inline-block; width:16px; height:16px; border-radius:50%; border:1px solid var(--paper-line); vertical-align:middle; }
.settings-danger { color: var(--hanko-dark); }

/* ---------- グラフ ---------- */
.chart-box { position: relative; width: 100%; height: 260px; }
.chart-box canvas { max-width: 100% !important; max-height: 100% !important; }
