/* ===== テーマ（data-theme で切替。デフォルトはダーク） ===== */
:root,
[data-theme="dark"] {
  --bg: #0e1014; --surface: #171a21; --surface2: #1f232c; --border: #2a2f3a; --line: #20242d;
  --text: #e8eaf0; --muted: #8b93a3; --accent: #6c8cff; --accent-text: #fff;
  --ink-mix: 0%; --today: rgba(108, 140, 255, .08); --shadow: 0 10px 30px rgba(0, 0, 0, .5);
  color-scheme: dark;
}
[data-theme="midnight"] {
  --bg: #0b1020; --surface: #121a33; --surface2: #1a2444; --border: #26335a; --line: #18213d;
  --text: #e6ebff; --muted: #8e9ac0; --accent: #5ea0ff; --accent-text: #fff;
  --ink-mix: 0%; --today: rgba(94, 160, 255, .09); --shadow: 0 10px 30px rgba(0, 0, 0, .55);
  color-scheme: dark;
}
[data-theme="forest"] {
  --bg: #0d1410; --surface: #142019; --surface2: #1b2b22; --border: #27392e; --line: #1a281f;
  --text: #e4efe7; --muted: #8aa595; --accent: #4fd18b; --accent-text: #08130c;
  --ink-mix: 0%; --today: rgba(79, 209, 139, .08); --shadow: 0 10px 30px rgba(0, 0, 0, .5);
  color-scheme: dark;
}
[data-theme="black"] {
  --bg: #000; --surface: #0b0b0b; --surface2: #151515; --border: #262626; --line: #161616;
  --text: #f2f2f2; --muted: #8a8a8a; --accent: #ff9f43; --accent-text: #160b00;
  --ink-mix: 0%; --today: rgba(255, 159, 67, .07); --shadow: 0 10px 30px rgba(0, 0, 0, .7);
  color-scheme: dark;
}
[data-theme="light"] {
  --bg: #f4f6fb; --surface: #fff; --surface2: #eef1f7; --border: #dde2ec; --line: #e8ecf3;
  --text: #1d2433; --muted: #6b7487; --accent: #4f6bed; --accent-text: #fff;
  --ink-mix: 38%; --today: rgba(79, 107, 237, .06); --shadow: 0 10px 30px rgba(20, 30, 60, .15);
  color-scheme: light;
}
[data-theme="sakura"] {
  --bg: #fff5f8; --surface: #fff; --surface2: #fde8ef; --border: #f3cfdb; --line: #fbe4ec;
  --text: #3a2630; --muted: #9a7482; --accent: #e75a8a; --accent-text: #fff;
  --ink-mix: 38%; --today: rgba(231, 90, 138, .06); --shadow: 0 10px 30px rgba(90, 30, 50, .15);
  color-scheme: light;
}
[data-theme="sepia"] {
  --bg: #f5efe3; --surface: #fffaf0; --surface2: #efe6d4; --border: #e0d4bd; --line: #ece3d1;
  --text: #3b3226; --muted: #857661; --accent: #b0793b; --accent-text: #fff;
  --ink-mix: 42%; --today: rgba(176, 121, 59, .07); --shadow: 0 10px 30px rgba(60, 40, 10, .15);
  color-scheme: light;
}

/* カテゴリ色の文字：明るいテーマでは黒を混ぜて読みやすくする */
.ink { color: color-mix(in srgb, var(--c), #000 var(--ink-mix)); }

* { box-sizing: border-box; }
[hidden] { display: none !important; }
html, body { height: 100%; }
body {
  margin: 0; background: var(--bg); color: var(--text);
  font-family: system-ui, -apple-system, "Segoe UI", "Hiragino Sans", "Noto Sans JP", "Yu Gothic UI", sans-serif;
  font-size: 14px; display: flex; flex-direction: column; height: 100dvh; overflow: hidden;
  -webkit-tap-highlight-color: transparent;
}
button, input, select, textarea { font: inherit; color: inherit; }
button { cursor: pointer; }
:focus-visible { outline: 2px solid var(--accent); outline-offset: 2px; }
::-webkit-scrollbar { width: 8px; height: 8px; }
::-webkit-scrollbar-thumb { background: var(--border); border-radius: 8px; }

.btn {
  border: 1px solid var(--border); background: var(--surface2); border-radius: 8px;
  padding: 7px 12px; white-space: nowrap; transition: filter .15s, background .15s;
}
.btn:hover { filter: brightness(1.12); }
.btn.sm { padding: 5px 10px; font-size: 13px; }
.btn.primary { background: var(--accent); border-color: var(--accent); color: var(--accent-text); font-weight: 600; }
.btn.danger { color: #ff6b6b; }
.btn:disabled { opacity: .5; cursor: default; }
.icon-btn {
  border: 0; background: transparent; width: 36px; height: 36px; border-radius: 8px;
  font-size: 18px; display: inline-grid; place-items: center; line-height: 1;
}
.icon-btn:hover { background: var(--surface2); }
.icon-btn.sm { width: 28px; height: 28px; font-size: 14px; }
input, select, textarea {
  background: var(--surface2); border: 1px solid var(--border); border-radius: 8px; padding: 7px 9px; min-width: 0;
}
textarea { resize: vertical; }

/* ===== ヘッダー ===== */
.topbar {
  display: flex; align-items: center; gap: 10px; padding: 8px 12px;
  padding-top: max(8px, env(safe-area-inset-top));
  background: var(--surface); border-bottom: 1px solid var(--border); flex-wrap: wrap;
}
.tb-nav { display: flex; align-items: center; gap: 4px; flex: 1; min-width: 0; }
.tb-nav h1 { font-size: 18px; margin: 0 0 0 8px; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.tb-tools { display: flex; align-items: center; gap: 4px; }
.seg { display: inline-flex; background: var(--surface2); border: 1px solid var(--border); border-radius: 10px; padding: 2px; }
.seg button { border: 0; background: transparent; padding: 5px 14px; border-radius: 8px; color: var(--muted); }
.seg button.active { background: var(--accent); color: var(--accent-text); font-weight: 600; }
.sync-dot { width: 9px; height: 9px; border-radius: 50%; background: transparent; margin: 0 4px; }
.sync-dot.ok { background: #4cd18a; }
.sync-dot.busy { background: #ffc93c; animation: pulse 1s infinite; }
.sync-dot.err { background: #ff5c6c; }
.sync-dot.local { background: var(--muted); }
@keyframes pulse { 50% { opacity: .3; } }

.banner {
  display: flex; align-items: center; gap: 10px; justify-content: space-between; flex-wrap: wrap;
  padding: 8px 14px; background: var(--surface2); border-bottom: 1px solid var(--border); font-size: 13px;
}
.banner code { background: var(--surface); padding: 1px 5px; border-radius: 4px; }

/* ===== レイアウト ===== */
.layout { flex: 1; display: flex; min-height: 0; position: relative; }
#calendar { flex: 1; min-width: 0; display: flex; flex-direction: column; overflow: hidden; touch-action: pan-y; }
.side {
  width: 300px; flex-shrink: 0; border-left: 1px solid var(--border); background: var(--surface);
  overflow-y: auto; display: flex; flex-direction: column;
}
body.side-hidden .side { display: none; }
.side-backdrop { display: none; }
.side-sec { padding: 12px; border-bottom: 1px solid var(--border); }
.side-head { display: flex; align-items: center; gap: 8px; margin-bottom: 8px; }
.side-head h2 { font-size: 15px; margin: 0; flex: 1; }
.side-head select { max-width: 140px; padding: 4px 6px; font-size: 13px; }
.side-close { display: none; }
.hint { color: var(--muted); font-size: 11px; }

/* ===== 時間グリッド（日・3日・週） ===== */
.tg { --hh: 48px; display: flex; flex-direction: column; height: 100%; min-height: 0; }
.tg-row { display: grid; grid-template-columns: 52px repeat(var(--n), minmax(0, 1fr)); }
.tg-head { border-bottom: 1px solid var(--border); background: var(--surface); }
.tg-dayhead {
  text-align: center; padding: 6px 2px; border-left: 1px solid var(--line); cursor: pointer; user-select: none;
}
.tg-dayhead .wd { font-size: 11px; color: var(--muted); }
.tg-dayhead .dn { font-size: 20px; font-weight: 600; width: 34px; height: 34px; line-height: 34px; margin: 2px auto 0; border-radius: 50%; }
.tg-dayhead.today .dn { background: var(--accent); color: var(--accent-text); }
.tg-dayhead.sun .wd, .mc-wd.sun { color: #ff7b86; }
.tg-dayhead.sat .wd, .mc-wd.sat { color: #6aa8ff; }
.tg-allday { border-bottom: 1px solid var(--border); max-height: 30%; overflow-y: auto; flex-shrink: 0; }
.tg-allday .lbl { font-size: 10px; color: var(--muted); text-align: right; padding: 4px 6px 0 0; }
.tg-adcol { border-left: 1px solid var(--line); padding: 2px; min-height: 26px; display: flex; flex-direction: column; gap: 2px; cursor: pointer; }
.tg-body { flex: 1; overflow-y: auto; position: relative; overscroll-behavior: contain; }
.tg-inner { height: calc(24 * var(--hh)); position: relative; }
.tg-hours { position: relative; }
.tg-hours span { position: absolute; right: 6px; font-size: 10px; color: var(--muted); transform: translateY(-50%); }
.tg-col {
  position: relative; border-left: 1px solid var(--line); cursor: pointer;
  background-image:
    linear-gradient(to bottom, var(--line) 1px, transparent 1px),
    linear-gradient(to bottom, transparent calc(var(--hh) / 2), color-mix(in srgb, var(--line) 55%, transparent) calc(var(--hh) / 2), color-mix(in srgb, var(--line) 55%, transparent) calc(var(--hh) / 2 + 1px), transparent calc(var(--hh) / 2 + 1px));
  background-size: 100% var(--hh);
}
.tg-col.today { background-color: var(--today); }
.now-line { position: absolute; left: 0; right: 0; height: 2px; background: #ff5c6c; z-index: 5; pointer-events: none; }
.now-line::before { content: ""; position: absolute; left: -5px; top: -4px; width: 10px; height: 10px; border-radius: 50%; background: #ff5c6c; }

.ev {
  position: absolute; border-radius: 6px; padding: 2px 5px; overflow: hidden; font-size: 12px; line-height: 1.3;
  background: color-mix(in srgb, var(--c) 18%, var(--surface)); border-left: 3px solid var(--c);
  cursor: pointer; z-index: 2; box-shadow: 0 1px 2px rgba(0, 0, 0, .2);
}
.ev:hover { z-index: 4; filter: brightness(1.1); }
.ev .ev-t { font-weight: 600; overflow: hidden; text-overflow: ellipsis; }
.ev .ev-tm { font-size: 11px; color: var(--muted); }
.ev.short .ev-tm { display: none; }
.ev.past { opacity: .6; }
.chip {
  display: flex; align-items: center; gap: 4px; font-size: 12px; padding: 2px 6px; border-radius: 5px;
  background: color-mix(in srgb, var(--c) 18%, var(--surface)); border-left: 3px solid var(--c);
  white-space: nowrap; overflow: hidden; text-overflow: ellipsis; cursor: pointer; min-width: 0;
}
.chip > span:last-child { overflow: hidden; text-overflow: ellipsis; }
.chip.task { --c: var(--muted); background: transparent; border: 1px dashed var(--border); }
.chip.task.done span:last-child { text-decoration: line-through; color: var(--muted); }
.chk {
  width: 14px; height: 14px; border: 1.5px solid var(--muted); border-radius: 4px; flex-shrink: 0;
  display: inline-grid; place-items: center; font-size: 10px; line-height: 1; background: transparent; padding: 0;
}
.chk.on { background: var(--accent); border-color: var(--accent); color: var(--accent-text); }

/* ===== 月表示 ===== */
.mg { display: flex; flex-direction: column; height: 100%; min-height: 0; }
.mg-head { display: grid; grid-template-columns: repeat(7, minmax(0, 1fr)); background: var(--surface); border-bottom: 1px solid var(--border); }
.mc-wd { text-align: center; font-size: 12px; color: var(--muted); padding: 6px 0; }
.mg-body { flex: 1; display: grid; grid-template-columns: repeat(7, minmax(0, 1fr)); grid-template-rows: repeat(6, minmax(0, 1fr)); min-height: 0; }
.mc {
  border-right: 1px solid var(--line); border-bottom: 1px solid var(--line); padding: 3px; min-height: 0; overflow: hidden;
  display: flex; flex-direction: column; gap: 2px; cursor: pointer;
}
.mc.other { background: color-mix(in srgb, var(--bg) 60%, transparent); }
.mc.other .mc-dn { opacity: .45; }
.mc.today { background: var(--today); }
.mc-dn { font-size: 12px; font-weight: 600; width: 22px; height: 22px; line-height: 22px; text-align: center; border-radius: 50%; flex-shrink: 0; }
.mc.today .mc-dn { background: var(--accent); color: var(--accent-text); }
.mc.sun .mc-dn { color: #ff7b86; } .mc.sat .mc-dn { color: #6aa8ff; }
.mc.today .mc-dn { color: var(--accent-text); }
.mc .chip { font-size: 11px; padding: 1px 4px; }
.mc .chip.timed { background: transparent; border-left: 0; padding-left: 1px; }
.mc .chip .tm { color: var(--muted); font-size: 10px; flex-shrink: 0; }
.mc-more { font-size: 11px; color: var(--muted); padding-left: 4px; }

/* ===== ToDo ===== */
.task-add { display: flex; gap: 6px; margin-bottom: 8px; }
.task-add #task-title { flex: 1; }
.task-add #task-due { width: 128px; padding: 6px 4px; }
.task-add #task-due:not(.has):not(:focus) { width: 42px; color: transparent; }
.tasks { list-style: none; margin: 0; padding: 0; display: flex; flex-direction: column; gap: 2px; }
.tasks li { display: flex; align-items: flex-start; gap: 8px; padding: 7px 6px; border-radius: 8px; cursor: pointer; }
.tasks li:hover { background: var(--surface2); }
.tasks li .chk { margin-top: 2px; width: 18px; height: 18px; font-size: 12px; }
.tasks .t-main { flex: 1; min-width: 0; }
.tasks .t-title { overflow-wrap: anywhere; }
.tasks .t-meta { font-size: 11px; color: var(--muted); display: flex; gap: 8px; flex-wrap: wrap; margin-top: 2px; }
.tasks .t-meta .over { color: #ff6b6b; }
.tasks li.done .t-title { text-decoration: line-through; color: var(--muted); }
.tasks .empty, .tasks .sub { color: var(--muted); font-size: 12px; cursor: default; }
.tasks .sub:hover { background: transparent; }
.tasks .sub button { border: 0; background: none; color: var(--muted); padding: 0; font-size: 12px; }

.cat-filter { display: flex; flex-wrap: wrap; gap: 6px; }
.cat-filter button {
  display: inline-flex; align-items: center; gap: 4px; border: 1px solid var(--border); background: var(--surface2);
  border-radius: 999px; padding: 4px 10px; font-size: 12px;
}
.cat-filter button.off { opacity: .35; text-decoration: line-through; }

/* ===== FAB / トースト / 通知 ===== */
.fab {
  position: fixed; right: 20px; bottom: max(20px, env(safe-area-inset-bottom)); width: 56px; height: 56px;
  border-radius: 50%; border: 0; background: var(--accent); color: var(--accent-text); font-size: 28px;
  box-shadow: var(--shadow); z-index: 30;
}
body:not(.side-hidden) .fab { right: 320px; }
.toast {
  position: fixed; left: 50%; bottom: 90px; transform: translateX(-50%); background: var(--text); color: var(--bg);
  padding: 9px 16px; border-radius: 10px; z-index: 100; max-width: 90vw; font-size: 13px; box-shadow: var(--shadow);
}
.alerts { position: fixed; top: 64px; right: 12px; z-index: 90; display: flex; flex-direction: column; gap: 8px; max-width: min(360px, 92vw); }
.alert {
  background: var(--surface); border: 1px solid var(--border); border-left: 4px solid var(--c, var(--accent));
  border-radius: 10px; padding: 10px 12px; box-shadow: var(--shadow); display: flex; gap: 10px; align-items: flex-start;
  animation: slidein .25s ease-out;
}
.alert .a-main { flex: 1; min-width: 0; }
.alert .a-title { font-weight: 700; }
.alert .a-sub { font-size: 12px; color: var(--muted); }
@keyframes slidein { from { transform: translateX(30px); opacity: 0; } }

/* ===== モーダル ===== */
dialog {
  border: 1px solid var(--border); border-radius: 14px; background: var(--surface); color: var(--text);
  padding: 0; width: min(560px, 94vw); max-height: 90dvh; box-shadow: var(--shadow);
}
dialog::backdrop { background: rgba(0, 0, 0, .55); }
.m-head { display: flex; align-items: center; gap: 8px; padding: 14px 16px 6px; }
.m-head h3 { margin: 0; font-size: 17px; flex: 1; }
.m-body { padding: 6px 16px 12px; display: flex; flex-direction: column; gap: 12px; overflow-y: auto; max-height: calc(90dvh - 130px); }
.m-foot { display: flex; gap: 8px; padding: 12px 16px; border-top: 1px solid var(--border); justify-content: flex-end; }
.m-foot .spacer { flex: 1; }
.field { display: flex; flex-direction: column; gap: 5px; }
.field > label, .field > .lbl { font-size: 12px; color: var(--muted); }
.row { display: flex; gap: 8px; align-items: center; flex-wrap: wrap; }
.row > input[type="date"] { flex: 1.3; min-width: 130px; }
.row > input[type="time"] { flex: 1; min-width: 100px; }
.title-input { font-size: 17px; padding: 9px 11px; }
.check { display: inline-flex; align-items: center; gap: 6px; cursor: pointer; }
.chips { display: flex; flex-wrap: wrap; gap: 6px; }
.chips button {
  border: 1px solid var(--border); background: var(--surface2); border-radius: 999px; padding: 5px 11px; font-size: 13px;
  display: inline-flex; align-items: center; gap: 4px;
}
.chips button.on { border-color: var(--c, var(--accent)); background: color-mix(in srgb, var(--c, var(--accent)) 22%, var(--surface2)); font-weight: 600; }
.icon-grid { display: grid; grid-template-columns: repeat(auto-fill, minmax(38px, 1fr)); gap: 4px; }
.icon-grid button { border: 1px solid transparent; background: var(--surface2); border-radius: 8px; height: 38px; font-size: 19px; padding: 0; }
.icon-grid button.on { border-color: var(--accent); background: color-mix(in srgb, var(--accent) 25%, var(--surface2)); }
.icon-grid button.auto { font-size: 11px; }
.color-grid { display: flex; flex-wrap: wrap; gap: 6px; align-items: center; }
.color-grid button { width: 28px; height: 28px; border-radius: 50%; border: 2px solid transparent; background: var(--c); padding: 0; }
.color-grid button.on { border-color: var(--text); box-shadow: 0 0 0 2px var(--surface) inset; }
.color-grid input[type="color"] { width: 34px; height: 30px; padding: 2px; }
.pick-btn { border: 1px solid var(--border); background: var(--surface2); border-radius: 8px; height: 38px; min-width: 44px; font-size: 20px; }
.ro-note { font-size: 12px; color: var(--muted); background: var(--surface2); padding: 8px 10px; border-radius: 8px; }

.set-sec { border: 1px solid var(--border); border-radius: 12px; padding: 12px; display: flex; flex-direction: column; gap: 10px; }
.set-sec h4 { margin: 0; font-size: 14px; }
.theme-grid { display: grid; grid-template-columns: repeat(auto-fill, minmax(110px, 1fr)); gap: 8px; }
.theme-grid button {
  border: 2px solid var(--border); border-radius: 10px; padding: 0; overflow: hidden; background: none; text-align: left;
}
.theme-grid button.on { border-color: var(--accent); }
.theme-prev { height: 44px; display: flex; gap: 4px; padding: 8px; }
.theme-prev i { flex: 1; border-radius: 4px; }
.theme-grid .tn { display: block; padding: 5px 8px; font-size: 12px; background: var(--surface2); }
.cat-row { display: grid; grid-template-columns: 44px 1fr 34px 34px; gap: 6px; align-items: center; }
.cat-row .kw { grid-column: 2 / 5; font-size: 12px; }
.cat-row .sw { width: 30px; height: 30px; border-radius: 50%; border: 2px solid var(--border); background: var(--c); padding: 0; }
.cat-item { border-bottom: 1px dashed var(--border); padding-bottom: 8px; display: flex; flex-direction: column; gap: 6px; }
.cat-item:last-child { border-bottom: 0; }
.cal-list { display: flex; flex-direction: column; gap: 6px; }
.cal-list .sw-dot { width: 12px; height: 12px; border-radius: 3px; background: var(--c); display: inline-block; }
.muted { color: var(--muted); font-size: 12px; }
.small-list { margin: 0; padding-left: 18px; color: var(--muted); font-size: 12px; line-height: 1.7; }

/* ===== スマホ ===== */
@media (max-width: 760px) {
  body { font-size: 15px; }
  .topbar { gap: 6px; padding: 6px 8px; padding-top: max(6px, env(safe-area-inset-top)); }
  .tb-nav { order: 1; }
  .tb-tools { order: 2; }
  .seg { order: 3; width: 100%; }
  .seg button { flex: 1; padding: 6px 0; }
  .tb-nav h1 { font-size: 16px; margin-left: 4px; }
  #account .btn { padding: 4px 8px; font-size: 12px; }
  .side {
    position: fixed; top: 0; right: 0; bottom: 0; width: min(380px, 92vw); z-index: 60;
    transform: translateX(100%); transition: transform .25s ease; box-shadow: var(--shadow);
    padding-top: env(safe-area-inset-top);
  }
  body.side-hidden .side { display: flex; }
  body:not(.side-hidden) .side { transform: none; }
  body:not(.side-hidden) .side-backdrop { display: block; position: fixed; inset: 0; background: rgba(0, 0, 0, .45); z-index: 55; }
  .side-close { display: inline-grid; }
  body:not(.side-hidden) .fab { right: 20px; }
  .alerts { top: auto; bottom: max(84px, calc(env(safe-area-inset-bottom) + 76px)); left: 12px; right: 12px; max-width: none; }
  .fab { right: 16px; bottom: max(16px, env(safe-area-inset-bottom)); }
  .tg-row { grid-template-columns: 40px repeat(var(--n), minmax(0, 1fr)); }
  .tg-dayhead .dn { font-size: 17px; width: 30px; height: 30px; line-height: 30px; }
  .ev { font-size: 11px; padding: 1px 3px; }
  .mc { padding: 2px 1px; }
  .mc .chip { font-size: 10px; padding: 0 2px; gap: 2px; }
  .mc .chip .tm { display: none; }
  .cat-row { grid-template-columns: 44px 1fr 34px 34px; }
  dialog { width: 100vw; max-width: 100vw; max-height: 94dvh; margin: auto 0 0; border-radius: 16px 16px 0 0; }
  .m-body { max-height: calc(94dvh - 130px); }
  .m-foot { padding-bottom: max(12px, env(safe-area-inset-bottom)); }
}
