:root {
  --bg: #f6f4ee;
  --surface: #ffffff;
  --ink: #2b2521;
  --ink-soft: #6b615a;
  --line: #e3e0d6;
  --primary: #ff9f1a;       /* 塗り用。明るいので上に載せる文字は --on-primary */
  --primary-press: #f59000;
  --on-primary: #2b1600;    /* 白文字だとコントラスト 2:1 で読めないため濃い茶色にする */
  --primary-ink: #a35c00;   /* 白や淡色の上に置くオレンジ文字用 */
  --primary-soft: #fff0d9;
  --bubble-me: #ffe8c2;
  --danger: #c0392b;
  --danger-soft: #fbeceb;
  --radius: 14px;
  --tabbar-h: 60px;
  --safe-bottom: env(safe-area-inset-bottom, 0px);
  --glass: rgba(255, 255, 255, .82);
  --glass-bg: rgba(246, 244, 238, .82);
  --halo: 0 0 3px var(--bg), 0 0 6px var(--bg);
  color-scheme: light;
}

* { box-sizing: border-box; }

html, body { height: 100%; }

body {
  margin: 0;
  background: var(--bg);
  color: var(--ink);
  font-family: -apple-system, BlinkMacSystemFont, "Hiragino Sans", "Hiragino Kaku Gothic ProN", "Noto Sans JP", sans-serif;
  font-size: 16px;
  line-height: 1.6;
  -webkit-text-size-adjust: 100%;
  -webkit-tap-highlight-color: transparent;
}

[hidden] { display: none !important; }

button, input, textarea { font: inherit; color: inherit; }

.visually-hidden {
  position: absolute; width: 1px; height: 1px; overflow: hidden;
  clip: rect(0 0 0 0); white-space: nowrap; opacity: 0;
}

/* ---------- 背景の ○ × △ □ ---------- */

.decor {
  position: fixed; inset: 0; z-index: 0;
  width: 100%; height: 100%;
  pointer-events: none;
  fill: none; stroke-width: 4.5; stroke-linecap: round; stroke-linejoin: round;
  opacity: .12;
  transition: opacity .3s;
}
/* ログイン画面は入力欄が1つだけで読ませる文字が少ないので、図形をほんの少しだけ濃くする */
body:has(#login-view:not([hidden])) .decor { opacity: .18; }

.decor-float {
  animation: decor-float 8s ease-in-out infinite alternate;
}
@keyframes decor-float {
  from { transform: translateY(-5px) rotate(-6deg); }
  to { transform: translateY(5px) rotate(6deg); }
}

/* 図形の上に直接乗る小さな文字は、背景色のにじみで読みやすさを保つ */
.msg-sender, .msg-time, .empty, .section-title, .hint, .select-loading, .size-summary,
.login-lead, .field-label, .editing-title, .done-lead, .done-file, .done-title, .login h1 {
  text-shadow: var(--halo);
}

/* ---------- 共通部品 ---------- */

.btn {
  display: inline-flex; align-items: center; justify-content: center; gap: 8px;
  min-height: 48px; padding: 0 20px;
  border: 0; border-radius: 12px;
  font-weight: 700; cursor: pointer;
  transition: background-color .15s, transform .1s;
}
.btn:active:not(:disabled) { transform: scale(.98); }
.btn:disabled { opacity: .45; cursor: default; }
.btn-primary { background: var(--primary); color: var(--on-primary); }
.btn-primary:active:not(:disabled) { background: var(--primary-press); }
.btn-secondary { background: var(--surface); color: var(--primary-ink); box-shadow: inset 0 0 0 1.5px var(--primary); }
.btn-block { width: 100%; }
.btn-large { min-height: 56px; font-size: 17px; }

.link-button {
  background: none; border: 0; padding: 8px; margin: -8px;
  color: var(--ink-soft); font-size: 14px; cursor: pointer;
}

.input {
  width: 100%;
  padding: 12px 14px;
  background: var(--surface);
  border: 1.5px solid var(--line);
  border-radius: 12px;
  font-size: 16px; /* 16px 未満だと iOS がズームする */
  outline: none;
}
.input:focus { border-color: var(--primary); box-shadow: 0 0 0 3px var(--primary-soft); }
textarea.input { resize: none; line-height: 1.5; }

.field-label { display: block; margin-bottom: 6px; font-size: 14px; font-weight: 700; }
.optional { font-weight: 400; font-size: 12px; color: var(--ink-soft); margin-left: 4px; }
.hint { margin: 4px 0 0; font-size: 12px; color: var(--ink-soft); }
.counter { text-align: right; }
.muted { color: var(--ink-soft); font-size: 13px; margin: 0; }

.field-error { min-height: 1.5em; margin: 6px 0 12px; color: var(--danger); font-size: 14px; }
.inline-error {
  margin: 12px 0 0; padding: 10px 12px;
  background: var(--danger-soft); color: var(--danger);
  border-radius: 10px; font-size: 14px;
}
.empty { text-align: center; color: var(--ink-soft); font-size: 14px; margin: 32px 0; }

/* ---------- ログイン ---------- */

.login {
  position: relative; z-index: 1;
  min-height: 100%;
  display: flex; align-items: center; justify-content: center;
  padding: 24px 20px calc(24px + var(--safe-bottom));
}
.login-card {
  width: 100%; max-width: 380px;
  padding: 28px 24px 20px;
  background: var(--glass);
  -webkit-backdrop-filter: blur(8px); backdrop-filter: blur(8px);
  border-radius: 24px;
  box-shadow: 0 10px 30px rgba(60, 38, 20, .08);
}
.login-mark {
  width: 64px; height: 64px; border-radius: 18px;
  display: grid; place-items: center;
  background: var(--primary); color: var(--on-primary); margin-bottom: 20px;
}
.login h1 { font-size: 24px; margin: 0 0 8px; }
.login-lead { margin: 0 0 28px; color: var(--ink-soft); }
.token-input { font-family: ui-monospace, "SF Mono", Menlo, monospace; letter-spacing: .06em; }

/* ---------- アプリ全体 ---------- */

.app {
  height: 100%;
  height: 100dvh;
  display: flex; flex-direction: column;
  max-width: 560px; margin: 0 auto;
  position: relative; z-index: 1;
}

.app-header {
  flex: none;
  display: flex; align-items: center; justify-content: space-between;
  padding: calc(10px + env(safe-area-inset-top, 0px)) 16px 10px;
  background: var(--glass);
  -webkit-backdrop-filter: blur(12px); backdrop-filter: blur(12px);
  border-bottom: 1px solid var(--line);
}
.app-header-name { font-weight: 700; font-size: 17px; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.honorific { font-weight: 400; font-size: 14px; color: var(--ink-soft); margin-left: 2px; }

.tab-panel { flex: 1; min-height: 0; }

.tabbar {
  flex: none;
  display: grid; grid-template-columns: 1fr 1fr;
  height: calc(var(--tabbar-h) + var(--safe-bottom));
  padding-bottom: var(--safe-bottom);
  background: var(--glass);
  -webkit-backdrop-filter: blur(12px); backdrop-filter: blur(12px);
  border-top: 1px solid var(--line);
}
.tab {
  display: flex; flex-direction: column; align-items: center; justify-content: center; gap: 2px;
  background: none; border: 0; cursor: pointer;
  color: var(--ink-soft); font-size: 12px; font-weight: 700;
}
.tab[aria-selected="true"] { color: var(--primary-ink); }

/* ---------- やりとり ---------- */

.messages-panel { display: flex; flex-direction: column; }

.message-list {
  flex: 1; min-height: 0;
  overflow-y: auto;
  -webkit-overflow-scrolling: touch;
  overscroll-behavior: contain;
  padding: 16px 12px 8px;
  display: flex; flex-direction: column; gap: 6px;
}

.day-divider {
  align-self: center;
  margin: 10px 0 4px;
  padding: 1px 12px;
  background: var(--glass); border-radius: 999px;
  font-size: 12px; color: var(--ink-soft);
}

.msg { display: flex; flex-direction: column; max-width: 80%; }
.msg-student { align-self: flex-end; align-items: flex-end; }
.msg-tutor { align-self: flex-start; align-items: flex-start; }
.msg-sender { font-size: 12px; color: var(--ink-soft); margin: 0 0 2px 4px; }
.bubble {
  padding: 9px 13px;
  border-radius: 18px;
  white-space: pre-wrap; overflow-wrap: anywhere;
}
.msg-student .bubble { background: var(--bubble-me); border-bottom-right-radius: 6px; }
.msg-tutor .bubble { background: var(--surface); border: 1px solid var(--line); border-bottom-left-radius: 6px; }
.msg-time { font-size: 11px; color: var(--ink-soft); margin: 2px 6px 0; }
.msg-pending .bubble { opacity: .6; }

/* アップロード完了の自動メッセージは会話ではないので、帯で区別する（spec §6.4） */
.system-msg {
  align-self: stretch;
  display: flex; align-items: center; gap: 10px;
  margin: 6px 0;
  font-size: 12px; color: var(--ink-soft);
}
.system-msg::before, .system-msg::after { content: ""; flex: 1; height: 1px; background: var(--line); }
.system-msg span {
  max-width: 80%;
  padding: 3px 12px;
  background: var(--primary-soft); color: var(--primary-ink);
  border-radius: 999px;
  text-align: center; overflow-wrap: anywhere;
}

.messages-panel .inline-error { margin: 0 12px 8px; }

.composer {
  flex: none;
  display: flex; align-items: flex-end; gap: 8px;
  padding: 8px 12px;
  background: var(--glass-bg);
  -webkit-backdrop-filter: blur(12px); backdrop-filter: blur(12px);
  border-top: 1px solid var(--line);
}
.composer-input { max-height: 140px; padding: 10px 14px; border-radius: 22px; }
.composer-send { flex: none; min-height: 44px; border-radius: 22px; padding: 0 18px; }

/* ---------- 提出 ---------- */

.submit-panel { overflow-y: auto; -webkit-overflow-scrolling: touch; }
.submit-state { padding: 20px 16px 32px; }

.big-actions { display: flex; flex-direction: column; gap: 12px; }
.big-button {
  display: flex; align-items: center; justify-content: center; gap: 12px;
  min-height: 72px;
  border-radius: var(--radius);
  font-size: 18px; font-weight: 700;
  cursor: pointer; user-select: none;
  transition: transform .1s, background-color .15s;
}
.big-button:active { transform: scale(.98); }
.big-button:focus-within { outline: 3px solid var(--primary-soft); outline-offset: 2px; }
.big-button-primary { background: var(--primary); color: var(--on-primary); min-height: 88px; font-size: 20px; }
.big-button-secondary { background: var(--surface); color: var(--primary-ink); box-shadow: inset 0 0 0 1.5px var(--primary); min-height: 64px; }

.shoot-hint {
  display: flex; align-items: center; gap: 12px;
  margin-top: 14px; padding: 12px 14px;
  background: var(--primary-soft); color: var(--primary-ink);
  border-radius: var(--radius);
}
.shoot-hint svg { flex: none; }
.shoot-hint p { margin: 0; font-size: 14px; line-height: 1.5; color: var(--ink); }
.shoot-hint strong { color: var(--primary-ink); }

.select-loading { margin: 12px 0 0; text-align: center; font-size: 14px; color: var(--ink-soft); }

.section-title { font-size: 15px; margin: 32px 0 8px; color: var(--ink-soft); }

.history-list { list-style: none; margin: 0; padding: 0; background: var(--surface); border-radius: var(--radius); overflow: hidden; }
.history-list:empty { display: none; }
.history-item { padding: 12px 14px; border-bottom: 1px solid var(--line); }
.history-item:last-child { border-bottom: 0; }
.history-name { margin: 0; font-weight: 700; font-size: 15px; overflow-wrap: anywhere; }
.history-meta { margin: 2px 0 0; font-size: 12px; color: var(--ink-soft); }
.history-note { margin: 4px 0 0; font-size: 13px; color: var(--ink-soft); overflow-wrap: anywhere; }

.editing-head { display: grid; grid-template-columns: 1fr auto 1fr; align-items: center; margin-bottom: 16px; }
.editing-head .link-button { justify-self: start; margin: 0; }
.editing-title { margin: 0; font-size: 17px; }

.error-card {
  margin-bottom: 16px; padding: 14px;
  background: var(--danger-soft); color: var(--danger);
  border-radius: var(--radius);
}
.error-card p { margin: 0 0 10px; font-size: 14px; font-weight: 700; }
.error-card .btn { width: 100%; }

.page-list { list-style: none; margin: 0; padding: 0; display: flex; flex-direction: column; gap: 10px; }
.page-item {
  display: grid; grid-template-columns: 88px 1fr auto; gap: 12px; align-items: center;
  padding: 10px;
  background: var(--surface); border-radius: var(--radius);
  border: 1px solid var(--line);
}
.page-thumb {
  width: 88px; height: 112px; object-fit: contain;
  background: #eceae3; border-radius: 8px;
}
.page-label { margin: 0; font-weight: 700; }
.page-size { margin: 0; font-size: 12px; color: var(--ink-soft); }
.page-controls { display: flex; flex-direction: column; gap: 6px; }
.icon-button {
  width: 44px; height: 44px;
  display: grid; place-items: center;
  background: var(--bg); border: 0; border-radius: 10px;
  color: var(--ink); cursor: pointer;
}
.icon-button:disabled { opacity: .3; cursor: default; }
.icon-button.danger { color: var(--danger); background: var(--danger-soft); }
.page-remove { margin-top: 8px; }

.add-page {
  display: flex; align-items: center; justify-content: center; gap: 8px;
  min-height: 56px; margin-top: 10px;
  border: 1.5px dashed var(--primary); border-radius: var(--radius);
  color: var(--primary-ink); font-weight: 700; cursor: pointer;
}

.pdf-preview {
  display: flex; align-items: center; gap: 12px;
  padding: 14px; background: var(--surface); border: 1px solid var(--line); border-radius: var(--radius);
  color: var(--primary);
}
.pdf-name { margin: 0; font-weight: 700; color: var(--ink); overflow-wrap: anywhere; }

.form-group { margin-top: 20px; }

.submit-bar { margin-top: 24px; }
.size-summary { margin: 0 0 8px; text-align: center; font-size: 14px; color: var(--ink-soft); }

/* ---------- 完了 ---------- */

.done { text-align: center; padding-top: 48px; }
.done-check { color: var(--primary); display: inline-block; }
.done-title { font-size: 24px; margin: 16px 0 8px; }
.done-file { margin: 0; font-weight: 700; overflow-wrap: anywhere; }
.done-lead { margin: 4px 0 32px; color: var(--ink-soft); }
.done-actions { display: flex; flex-direction: column; gap: 10px; }

.done.animate { animation: fade-in .2s ease-out both; }
.done.animate .done-check { animation: pop .35s cubic-bezier(.2, 1.4, .4, 1) both; }

@keyframes fade-in { from { opacity: 0; } to { opacity: 1; } }
@keyframes pop { from { transform: scale(.6); } to { transform: scale(1); } }

/* ---------- アップロード中 ---------- */

.overlay {
  position: fixed; inset: 0; z-index: 100;
  display: grid; place-items: center;
  padding: 24px;
  background: rgba(40, 26, 16, .55);
  touch-action: none;
}
.overlay-card {
  width: 100%; max-width: 320px;
  padding: 28px 20px;
  background: var(--surface); border-radius: 18px;
  text-align: center;
}
.overlay-text { margin: 16px 0 6px; font-weight: 700; }
.overlay-note { margin: 0; font-size: 12px; color: var(--ink-soft); }
.spinner {
  width: 40px; height: 40px; margin: 0 auto;
  border: 4px solid var(--primary-soft); border-top-color: var(--primary);
  border-radius: 50%;
  animation: spin .9s linear infinite;
}
@keyframes spin { to { transform: rotate(360deg); } }

@media (prefers-reduced-motion: reduce) {
  *, *::before, *::after { animation: none !important; transition: none !important; }
  .spinner { border-top-color: var(--primary); }
}
