:root {
  --bg: #faf7f2;
  --card: #ffffff;
  --ink: #2f2a26;
  --sub: #7a716a;
  --line: #ebe4da;
  --accent: #b5866a;
  --accent-dark: #93674e;
  --danger: #b4483c;
  --radius: 14px;
}
* { box-sizing: border-box; }
html, body { margin: 0; }
body {
  background: var(--bg);
  color: var(--ink);
  font-family: -apple-system, BlinkMacSystemFont, "Hiragino Sans", "Hiragino Kaku Gothic ProN", "Noto Sans JP", "Yu Gothic", sans-serif;
  font-size: 16px;
  line-height: 1.7;
  -webkit-text-size-adjust: 100%;
  min-height: 100vh;
  display: flex;
  flex-direction: column;
}
[hidden] { display: none !important; }

.topbar {
  display: flex; align-items: center; justify-content: space-between;
  padding: 14px 20px; padding-top: max(14px, env(safe-area-inset-top));
  border-bottom: 1px solid var(--line); background: var(--bg);
  position: sticky; top: 0; z-index: 5;
}
.brand { display: flex; align-items: center; gap: 10px; text-decoration: none; color: var(--accent); font-size: 18px; letter-spacing: .06em; }
.brand img { width: 40px; height: 40px; display: block; }

main { flex: 1; width: 100%; max-width: 640px; margin: 0 auto; padding: 24px 16px 40px; }

.card {
  background: var(--card); border-radius: var(--radius); padding: 28px 22px;
  box-shadow: 0 1px 3px rgba(60, 40, 20, .06);
}
h1 { font-size: 21px; margin: 0 0 8px; font-weight: 600; }
.lead { color: var(--sub); margin: 0 0 20px; font-size: 15px; }
.lead strong { color: var(--ink); word-break: break-all; }
.note { color: var(--sub); font-size: 13px; margin: 16px 0 0; }

label { display: block; font-size: 14px; color: var(--sub); margin-bottom: 6px; }
input[type=email], input[type=text] {
  width: 100%; font-size: 17px; padding: 13px 14px;
  border: 1px solid var(--line); border-radius: 10px; background: #fff; color: var(--ink);
  -webkit-appearance: none; appearance: none;
}
input:focus { outline: 2px solid var(--accent); outline-offset: 1px; border-color: transparent; }
.code-input { font-size: 26px !important; letter-spacing: .5em; text-align: center; font-variant-numeric: tabular-nums; }
.check { display: flex; gap: 10px; align-items: flex-start; margin: 16px 0 4px; color: var(--ink); font-size: 14px; }
.check input { width: 20px; height: 20px; margin-top: 3px; accent-color: var(--accent); flex-shrink: 0; }

button { font: inherit; cursor: pointer; }
.primary {
  width: 100%; margin-top: 18px; padding: 14px; border: none; border-radius: 10px;
  background: var(--accent); color: #fff; font-size: 16px; font-weight: 600;
}
.primary:active { background: var(--accent-dark); }
.primary:disabled { opacity: .5; }
.link-btn { background: none; border: none; color: var(--accent-dark); padding: 6px 0; font-size: 14px; text-decoration: underline; text-underline-offset: 3px; }
.row { display: flex; justify-content: space-between; margin-top: 14px; }

.message {
  margin-top: 16px; padding: 12px 14px; border-radius: 10px; font-size: 14px;
  background: #fff4f2; color: var(--danger); border: 1px solid #f2d6d1;
}
.message.info { background: #f4f8f3; color: #3f6b3a; border-color: #d8e6d5; }

/* 一覧 */
.plan-head { margin: 4px 4px 18px; }
.plan-label { display: inline-block; margin: 0 0 6px; padding: 2px 10px; border-radius: 99px; background: #f1e7dd; color: var(--accent-dark); font-size: 12px; font-weight: 600; }
.group { background: var(--card); border-radius: var(--radius); margin-bottom: 12px; overflow: hidden; box-shadow: 0 1px 3px rgba(60, 40, 20, .06); }
.group > summary {
  list-style: none; padding: 16px 18px; font-weight: 600; display: flex; justify-content: space-between; align-items: center; gap: 10px;
}
.group > summary::-webkit-details-marker { display: none; }
.group > summary::after { content: "＋"; color: var(--accent); font-weight: 400; }
.group[open] > summary::after { content: "－"; }
.group .count { color: var(--sub); font-weight: 400; font-size: 13px; margin-left: auto; }
.group .group { box-shadow: none; border-top: 1px solid var(--line); border-radius: 0; margin: 0; background: #fcfaf7; }
.group .group > summary { padding-left: 30px; font-weight: 500; font-size: 15px; }
.items { list-style: none; margin: 0; padding: 0; }
.items li { border-top: 1px solid var(--line); }
.item-btn {
  width: 100%; display: flex; align-items: center; gap: 12px; text-align: left;
  background: none; border: none; padding: 14px 18px; color: var(--ink); font-size: 15px;
}
.item-btn:active { background: #f7f1ea; }
.badge { flex-shrink: 0; font-size: 11px; font-weight: 700; padding: 3px 0; width: 44px; text-align: center; border-radius: 6px; color: #fff; }
.badge.pdf { background: #c26a5a; }
.badge.image { background: #6f9a8d; }
.badge.sheet { background: #5c9466; }
.badge.doc { background: #6a83b8; }
.badge.youtube, .badge.video { background: #a05bb0; }
.empty { color: var(--sub); padding: 14px 18px; font-size: 14px; border-top: 1px solid var(--line); }

/* 閲覧画面 */
.viewer { position: fixed; inset: 0; top: var(--topbar-h, 0px); background: #f2eee8; z-index: 20; display: flex; flex-direction: column; }
.viewer-bar {
  display: flex; align-items: center; gap: 8px; background: #fff; border-bottom: 1px solid var(--line);
  padding: 10px 12px;
}
.close { border: none; background: none; font-size: 22px; padding: 4px 10px; color: var(--ink); }
.viewer-title { font-size: 15px; font-weight: 600; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.viewer-body { flex: 1; overflow: auto; -webkit-overflow-scrolling: touch; padding: 12px 8px max(24px, env(safe-area-inset-bottom)); }
.viewer-body canvas, .viewer-body img.full { display: block; width: 100%; max-width: 900px; margin: 0 auto 10px; background: #fff; box-shadow: 0 1px 4px rgba(0,0,0,.08); }
.video-wrap { position: relative; padding-top: 56.25%; max-width: 900px; margin: 0 auto; }
.video-wrap iframe { position: absolute; inset: 0; width: 100%; height: 100%; border: 0; border-radius: 8px; }
.viewer-note { max-width: 640px; margin: 40px auto; text-align: center; color: var(--sub); }

.tabs { display: flex; gap: 6px; overflow-x: auto; padding: 0 4px 10px; }
.tabs button { flex-shrink: 0; border: 1px solid var(--line); background: #fff; border-radius: 99px; padding: 6px 14px; font-size: 13px; }
.tabs button.active { background: var(--accent); color: #fff; border-color: var(--accent); }
.table-wrap { overflow: auto; background: #fff; border-radius: 8px; }
table { border-collapse: collapse; font-size: 13px; min-width: 100%; }
td { border: 1px solid var(--line); padding: 6px 8px; white-space: pre-wrap; vertical-align: top; min-width: 80px; }
tr:first-child td { background: #f7f1ea; font-weight: 600; position: sticky; top: 0; }

.loading { position: fixed; inset: 0; background: rgba(250,247,242,.85); z-index: 30; display: flex; flex-direction: column; align-items: center; justify-content: center; gap: 12px; color: var(--sub); font-size: 14px; }
.spinner { width: 36px; height: 36px; border: 3px solid var(--line); border-top-color: var(--accent); border-radius: 50%; animation: spin .8s linear infinite; }
@keyframes spin { to { transform: rotate(360deg); } }

.viewer-empty { display: none; }
.item-btn.active { background: #f3e9df; font-weight: 600; }

/* ---------- PC（幅960px以上）：左に一覧、右に資料 ---------- */
@media (min-width: 960px) {
  .topbar { padding-left: 32px; padding-right: 32px; }
  main { max-width: 1320px; padding: 28px 32px 32px; }
  #viewEmail, #viewCode { max-width: 440px; margin: 48px auto 0; padding: 36px 32px; }
  main > .message { max-width: 440px; margin-left: auto; margin-right: auto; }
  #viewLibrary ~ .message { max-width: none; }

  .lib-layout { display: grid; grid-template-columns: 380px minmax(0, 1fr); gap: 28px; align-items: start; }
  .lib-side { position: sticky; top: 84px; max-height: calc(100vh - 110px); overflow-y: auto; padding-right: 4px; }
  .item-btn:hover { background: #f7f1ea; }

  .viewer, .viewer-empty {
    position: sticky; inset: auto; top: 84px; z-index: auto;
    height: calc(100vh - 110px); border-radius: var(--radius); overflow: hidden;
    box-shadow: 0 1px 3px rgba(60, 40, 20, .08);
  }
  .viewer-bar { padding-top: 10px; }
  .viewer-body { padding: 20px; }
  #viewer[hidden] + .viewer-empty {
    display: flex; align-items: center; justify-content: center;
    background: #f2eee8; color: var(--sub); font-size: 15px; border: 2px dashed var(--line); box-shadow: none;
  }
  .footer { padding-top: 8px; }
}

.footer { text-align: center; color: var(--sub); font-size: 12px; padding: 20px 0 max(20px, env(safe-area-inset-bottom)); }
