:root {
  --bg: #f5f6fa;
  --surface: #ffffff;
  --text: #1c1f2a;
  --muted: #6b7080;
  --border: #e2e4ec;
  --primary: #4f46e5;
  --primary-ink: #ffffff;
  --primary-soft: #eef0ff;
  --danger: #dc2626;
  --ok: #16a34a;
  --radius: 12px;
}
@media (prefers-color-scheme: dark) {
  :root {
    --bg: #12141b;
    --surface: #1c1f29;
    --text: #e8e9ef;
    --muted: #9aa0b2;
    --border: #2e3240;
    --primary: #818cf8;
    --primary-ink: #111322;
    --primary-soft: #262a45;
    --danger: #f87171;
    --ok: #4ade80;
  }
}

* { box-sizing: border-box; }
html { -webkit-text-size-adjust: 100%; }
body {
  margin: 0;
  background: var(--bg);
  color: var(--text);
  font-family: system-ui, -apple-system, "Hiragino Sans", "Yu Gothic UI", "Noto Sans JP", sans-serif;
  line-height: 1.5;
  padding-bottom: env(safe-area-inset-bottom);
}
[hidden] { display: none !important; }
h1, h2, h3 { margin: 0; }
h1 { font-size: 1.1rem; }
h2 { font-size: 1.05rem; }
h3 { font-size: .95rem; font-weight: 600; }

.topbar {
  position: sticky; top: 0; z-index: 5;
  display: flex; align-items: center; justify-content: space-between; gap: 12px;
  padding: calc(10px + env(safe-area-inset-top)) 16px 10px;
  background: var(--surface);
  border-bottom: 1px solid var(--border);
}
.user-box { display: flex; align-items: center; gap: 8px; font-size: .9rem; color: var(--muted); }

main { max-width: 760px; margin: 0 auto; padding: 16px; }

.card {
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  padding: 16px;
  margin-bottom: 12px;
}
.row { display: flex; align-items: center; justify-content: space-between; gap: 12px; }
.row + .row { margin-top: 14px; padding-top: 14px; border-top: 1px solid var(--border); }
.muted { color: var(--muted); }
.small-text { font-size: .82rem; margin: 2px 0 0; }
.error { color: var(--danger); font-size: .88rem; margin: 0; }

button, .btn-primary, .btn-secondary {
  font: inherit; cursor: pointer; border-radius: 10px; border: 1px solid transparent;
  padding: 10px 16px; text-decoration: none; display: inline-flex; align-items: center; justify-content: center;
}
.btn-primary { background: var(--primary); color: var(--primary-ink); font-weight: 600; }
.btn-secondary { background: var(--primary-soft); color: var(--primary); border-color: var(--border); }
.btn-danger { background: transparent; color: var(--danger); border-color: var(--danger); }
.btn-text { background: none; border: none; color: var(--primary); padding: 4px 6px; }
.small { padding: 7px 12px; font-size: .85rem; }
button:disabled { opacity: .5; cursor: default; }

input, select {
  font: inherit; color: var(--text); background: var(--bg);
  border: 1px solid var(--border); border-radius: 10px; padding: 10px 12px; width: 100%;
}
select { width: auto; }
input:focus, select:focus { outline: 2px solid var(--primary); outline-offset: 1px; }
label { display: grid; gap: 6px; font-size: .88rem; color: var(--muted); }
label.check { display: flex; align-items: center; gap: 8px; color: var(--text); }
label.check input { width: auto; }

/* 認証 */
.auth { max-width: 380px; margin: 40px auto; }
.auth form { display: grid; gap: 14px; margin-top: 16px; }
.hint { font-size: .82rem; color: var(--muted); margin: 16px 0 0; }

/* タブ */
.tabs { display: grid; grid-template-columns: repeat(3, 1fr); gap: 4px; margin-bottom: 14px;
        background: var(--surface); border: 1px solid var(--border); border-radius: var(--radius); padding: 4px; }
.tabs button { background: none; color: var(--muted); padding: 9px 4px; }
.tabs button.active { background: var(--primary-soft); color: var(--primary); font-weight: 600; }

.banner { background: var(--primary-soft); color: var(--text); border: 1px solid var(--border);
          border-radius: var(--radius); padding: 12px 14px; margin-bottom: 12px; font-size: .9rem;
          display: flex; gap: 10px; align-items: center; justify-content: space-between; }

/* 今日 */
.today-head { display: flex; align-items: center; justify-content: space-between; gap: 10px; margin-bottom: 12px; flex-wrap: wrap; }
.today-list { list-style: none; margin: 0; padding: 0; display: grid; gap: 10px; }
.today-item { display: grid; grid-template-columns: 64px 1fr auto; gap: 12px; align-items: center;
              background: var(--surface); border: 1px solid var(--border); border-radius: var(--radius); padding: 12px 14px; }
.today-item.now { border-color: var(--primary); box-shadow: 0 0 0 1px var(--primary); }
.today-item.past { opacity: .55; }
.today-item .time { font-variant-numeric: tabular-nums; font-weight: 700; }
.today-item .time small { display: block; font-weight: 400; color: var(--muted); font-size: .75rem; }
.today-item .name { font-weight: 600; }
.today-item .meta { font-size: .8rem; color: var(--muted); }
.badge { font-size: .7rem; padding: 1px 7px; border-radius: 999px; border: 1px solid var(--border); margin-left: 6px; }
.badge.ok { color: var(--ok); border-color: var(--ok); }

.dot { display: inline-block; width: 7px; height: 7px; border-radius: 50%; background: var(--muted); vertical-align: middle; }

/* スイッチ */
.switch { position: relative; display: inline-block; width: 50px; height: 30px; flex: none; }
.switch input { opacity: 0; width: 0; height: 0; }
.switch span { position: absolute; inset: 0; background: var(--border); border-radius: 999px; transition: .2s; cursor: pointer; }
.switch span::before { content: ""; position: absolute; width: 24px; height: 24px; left: 3px; top: 3px;
                       background: #fff; border-radius: 50%; transition: .2s; box-shadow: 0 1px 2px rgb(0 0 0 / .2); }
.switch input:checked + span { background: var(--primary); }
.switch input:checked + span::before { transform: translateX(20px); }
.switch input:disabled + span { opacity: .5; cursor: default; }

.device-list { list-style: none; margin: 10px 0 0; padding: 0; display: grid; gap: 8px; }
.device-list li { display: flex; justify-content: space-between; align-items: center; gap: 10px; font-size: .85rem; }
.device-list .this { color: var(--primary); font-weight: 600; }

.help summary { cursor: pointer; font-weight: 600; font-size: .95rem; }
.help ol { padding-left: 1.3em; margin: 10px 0 6px; font-size: .9rem; }

/* ダイアログ */
dialog { border: none; border-radius: 16px; padding: 0; width: min(420px, calc(100vw - 32px));
         background: var(--surface); color: var(--text); }
dialog::backdrop { background: rgb(0 0 0 / .4); }
dialog form { display: grid; gap: 14px; padding: 20px; }
.dialog-actions { display: flex; gap: 8px; align-items: center; }
.spacer { flex: 1; }
.url-preview { word-break: break-all; }

.toast { position: fixed; left: 50%; bottom: calc(24px + env(safe-area-inset-bottom)); transform: translateX(-50%);
         background: var(--text); color: var(--bg); padding: 10px 16px; border-radius: 10px; font-size: .9rem;
         max-width: calc(100vw - 32px); z-index: 20; }

/* --- attendance & multi-period --- */
.auth h2 { font-size: 1rem; margin-bottom: 4px; }
.btn-primary.block { display: flex; width: 100%; margin-top: 14px; }
.banner.warn { border-color: var(--danger); }
.today-item .status { display: grid; justify-items: end; gap: 6px; }
.check-mark { display: inline-flex; align-items: center; justify-content: center; width: 28px; height: 28px;
              border-radius: 50%; background: var(--ok); color: #fff; font-weight: 700; font-size: .95rem; }
.today-item.attended { border-color: var(--ok); }
.badge.absent { color: var(--danger); border-color: var(--danger); }
.badge.warn { color: var(--danger); border-color: var(--danger); }
.tag-auto { font-size: .62rem; padding: 0 4px; border-radius: 4px; background: var(--border); color: var(--muted); }

/* --- timetable: calendar style (drag to select) --- */
.tt { --row: 58px; user-select: none; -webkit-user-select: none; -webkit-touch-callout: none;
      -webkit-tap-highlight-color: transparent; }
.tt-head, .tt-body { display: grid; grid-template-columns: 30px repeat(6, minmax(0, 1fr)); column-gap: 3px; }
.tt-head { position: sticky; top: calc(49px + env(safe-area-inset-top)); z-index: 3; background: var(--bg); padding: 4px 0; }
.tt-head div { text-align: center; font-size: .8rem; font-weight: 600; color: var(--muted); }
.tt-head .today { color: var(--primary); }
.tt-times { display: grid; grid-template-rows: repeat(10, var(--row)); }
.tt-time { display: flex; flex-direction: column; align-items: center; padding-top: 3px; line-height: 1.15;
           font-size: .78rem; color: var(--muted); }
.tt-time small { font-size: .6rem; }
.tt-col { position: relative; height: calc(var(--row) * 10); border-radius: 8px; border: 1px solid var(--border);
          background-color: var(--surface);
          background-image: repeating-linear-gradient(to bottom, transparent 0, transparent calc(var(--row) - 1px),
                            var(--border) calc(var(--row) - 1px), var(--border) var(--row)); }
.tt-col.today { box-shadow: inset 0 0 0 1px var(--primary); }
.tt-empty { position: absolute; left: 0; right: 0; padding: 0; border: 0; border-radius: 0; background: transparent;
            color: transparent; font-size: 1rem; cursor: cell; touch-action: manipulation; }
.tt-empty:hover, .tt-empty:focus-visible { color: var(--muted); background: var(--primary-soft); }
.tt-empty:focus-visible { outline: 2px solid var(--primary); outline-offset: -2px; }
.tt-slot { position: absolute; left: 2px; right: 2px; margin-top: 2px; padding: 4px 3px 14px;
           border-radius: 7px; border: 0; border-left: 3px solid var(--primary); background: var(--primary-soft);
           color: var(--text); display: flex; flex-direction: column; align-items: flex-start; justify-content: flex-start;
           gap: 1px; overflow: hidden; text-align: left; font-size: .72rem; line-height: 1.2; cursor: pointer;
           box-sizing: border-box; touch-action: manipulation; }
.tt-slot .room { font-weight: 700; font-size: .82rem; font-variant-numeric: tabular-nums; }
.tt-slot.noroom { border-left-color: var(--danger); }
.tt-slot.noroom .room { color: var(--danger); font-size: .68rem; }
.tt-slot.off { opacity: .6; }
.tt-slot .subj { color: var(--muted); font-size: .66rem; width: 100%; overflow: hidden;
                 display: -webkit-box; -webkit-line-clamp: 3; -webkit-box-orient: vertical; word-break: break-all; }
.tt-slot .icons { display: flex; gap: 3px; align-items: center; min-height: 12px; }
.tt-slot .check { color: var(--ok); font-weight: 700; font-size: .75rem; }
.tt-slot .grip { position: absolute; left: 0; right: 0; bottom: 0; height: 16px; cursor: ns-resize; touch-action: none;
                 display: flex; align-items: center; justify-content: center; }
.tt-slot .grip::after { content: ""; display: inline-block; width: 18px; height: 4px; border-radius: 2px;
                        background: var(--primary); opacity: .55; vertical-align: middle; }
.tt-sel { position: absolute; left: 2px; right: 2px; z-index: 2; pointer-events: none; border-radius: 7px;
          border: 2px solid var(--primary); background: color-mix(in srgb, var(--primary) 22%, transparent);
          color: var(--primary); font-size: .66rem; font-weight: 700; padding: 3px; white-space: pre-line; line-height: 1.25; }
.tt-col.dragging .tt-empty:hover { background: transparent; color: transparent; }
body.tt-dragging { cursor: ns-resize; overscroll-behavior: none; }

.room-btn { font: inherit; font-size: .8rem; color: var(--primary); background: none; border: 0; padding: 6px 8px 6px 0;
            margin-right: 6px; text-decoration: underline dotted; text-underline-offset: 3px; cursor: pointer; }
.room-btn::after { content: " \270E"; text-decoration: none; }
.room-btn.unset { color: var(--danger); }

/* --- phones --- */
@media (max-width: 480px) {
  main { padding: 12px; }
  .tt { --row: 60px; }
  .tt-head, .tt-body { grid-template-columns: 26px repeat(6, minmax(0, 1fr)); column-gap: 2px; }
  .today-item { grid-template-columns: 52px 1fr auto; gap: 10px; padding: 10px 12px; }
  /* dialog becomes a bottom sheet */
  dialog { width: 100%; max-width: 100%; margin: auto 0 0; border-radius: 16px 16px 0 0;
           max-height: 90dvh; overflow: auto; padding-bottom: env(safe-area-inset-bottom); }
  dialog form { padding: 18px 16px 20px; }
  .dialog-actions button { padding: 12px 14px; }
  input, select { font-size: 16px; } /* prevent iOS zoom on focus */
}

/* --- terms & excel import --- */
.file-btn { cursor: pointer; white-space: nowrap; }
.file-btn:focus-within { outline: 2px solid var(--primary); outline-offset: 2px; }
.empty-hint p { margin: 0 0 6px; }
.slot-info { margin: -4px 0 0; }
.warn-text { color: var(--danger); margin: 0 0 4px; }
.tt-sel { overflow: hidden; }
.import-preview { max-height: 50dvh; overflow: auto; display: grid; gap: 14px;
                  border: 1px solid var(--border); border-radius: 10px; padding: 10px 12px; }
.import-preview h4 { margin: 0 0 6px; font-size: .92rem; }
.import-preview ul { list-style: none; margin: 0; padding: 0; display: grid; gap: 4px; }
.import-preview li { display: grid; grid-template-columns: 6.5em 1fr auto; gap: 8px; font-size: .82rem; align-items: baseline; }
.import-preview .when { color: var(--muted); font-variant-numeric: tabular-nums; }
.import-preview .where { font-weight: 600; font-variant-numeric: tabular-nums; }
.import-preview .where.none { color: var(--danger); font-weight: 400; font-size: .75rem; }
@media (max-width: 480px) {
  .import-preview li { grid-template-columns: 5.5em 1fr; }
  .import-preview .where { grid-column: 2; }
}
.room-btn.add { color: var(--primary); font-weight: 600; text-decoration: none; }
.room-btn.add::after { content: none; }

/* --- in-app attendance --- */
.attend-target { border: 1px solid var(--border); border-radius: 10px; padding: 10px 12px; background: var(--bg); }
.attend-subject { font-weight: 700; }
.attend-result { margin: 0; padding: 10px 12px; border-radius: 10px; font-size: .88rem; }
.attend-result.error { color: var(--danger); border: 1px solid var(--danger); }
.attend-result.warn { border: 1px solid var(--border); background: var(--primary-soft); }
#attend-fallback { color: var(--primary); }
