:root {
  --paper: #ffffff;
  --bg: #f5f5f4;
  --fill: #f0f0ee;
  --line: #e4e4e2;
  --line-2: #efefed;
  --ink: #1b1b1a;
  --ink-2: #6d6d6a;
  --ink-3: #a0a09c;
  --danger: #9a3b3b;
  --radius: 10px;
  --safe-t: env(safe-area-inset-top, 0px);
  --safe-b: env(safe-area-inset-bottom, 0px);
}

html {
  color-scheme: light;
  -webkit-text-size-adjust: 100%;
}

/* hidden属性は display: none だが、作者スタイルの display 指定に負ける。
   .viewer や .login のように display を持つ要素が消えなくなるため明示する。 */
[hidden] { display: none !important; }

* {
  box-sizing: border-box;
  -webkit-tap-highlight-color: transparent;
}

body {
  margin: 0;
  background: var(--bg);
  color: var(--ink);
  font-family: "Noto Sans JP", system-ui, -apple-system, "Hiragino Sans", sans-serif;
  font-size: 15px;
  line-height: 1.55;
  -webkit-font-smoothing: antialiased;
  overscroll-behavior-y: none;
}

button { font: inherit; cursor: pointer; }

:focus-visible {
  outline: 2px solid var(--ink);
  outline-offset: 2px;
}

svg { width: 22px; height: 22px; fill: none; stroke: currentColor; stroke-width: 1.7; stroke-linecap: round; stroke-linejoin: round; }

/* ---------- 部品 ---------- */

.field {
  width: 100%;
  font: inherit;
  font-size: 16px;            /* 16px未満だとiOSが勝手に拡大する */
  color: var(--ink);
  background: var(--fill);
  border: 1px solid transparent;
  border-radius: 8px;
  padding: 11px 12px;
  appearance: none;
}
.field::placeholder { color: var(--ink-3); }
.field:focus {
  outline: none;
  background: var(--paper);
  border-color: var(--ink);
}

textarea.field { resize: vertical; min-height: 76px; line-height: 1.6; }

select.field {
  background-image: url("data:image/svg+xml;utf8,<svg xmlns='http://www.w3.org/2000/svg' viewBox='0 0 24 24' fill='none' stroke='%236d6d6a' stroke-width='1.7' stroke-linecap='round'><path d='M7 10l5 5 5-5'/></svg>");
  background-repeat: no-repeat;
  background-position: right 10px center;
  background-size: 20px;
  padding-right: 36px;
}

.btn {
  border: none;
  border-radius: 8px;
  padding: 12px 16px;
  min-height: 44px;
  font-size: 15px;
  font-weight: 500;
  background: var(--fill);
  color: var(--ink);
}
.btn--primary { background: var(--ink); color: #fff; }
.btn--plain { background: none; padding: 8px; min-height: 40px; }
.btn--block { display: block; width: 100%; }
.btn--danger { background: none; color: var(--danger); }
.btn:disabled { opacity: .45; cursor: default; }

.iconbtn {
  display: grid;
  place-items: center;
  width: 44px;
  height: 44px;
  border: none;
  background: none;
  color: var(--ink);
  border-radius: 8px;
}

.row { display: flex; gap: 8px; }
.row > * { flex: 1; }

.label {
  display: block;
  font-size: 13px;
  color: var(--ink-2);
  margin: 16px 0 6px;
}

.hint { font-size: 12px; color: var(--ink-2); margin: 6px 0 0; }

.spinner {
  display: block;
  width: 22px; height: 22px;
  border: 2px solid var(--line);
  border-top-color: var(--ink-3);
  border-radius: 50%;
  animation: spin .7s linear infinite;
}
@keyframes spin { to { transform: rotate(360deg); } }

/* ---------- 起動・ログイン ---------- */

.boot { display: grid; place-items: center; min-height: 100vh; min-height: 100dvh; }

.login { display: grid; place-items: center; min-height: 100vh; min-height: 100dvh; padding: 24px; }
.login__inner { width: 100%; max-width: 320px; }
.login__title { font-size: 26px; font-weight: 600; margin: 0 0 4px; letter-spacing: .02em; }
.login__lead { margin: 0 0 20px; color: var(--ink-2); font-size: 14px; }
.login__form { display: grid; gap: 10px; }
.login__error { margin: 12px 0 0; color: var(--danger); font-size: 13px; }

/* ---------- ヘッダー ---------- */

.topbar {
  position: sticky;
  top: 0;
  z-index: 20;
  padding-top: var(--safe-t);
  background: rgba(255, 255, 255, .9);
  backdrop-filter: saturate(1.6) blur(12px);
  -webkit-backdrop-filter: saturate(1.6) blur(12px);
  border-bottom: 1px solid var(--line);
}

.topbar__row {
  display: flex;
  align-items: center;
  gap: 8px;
  padding: 6px 10px 6px 16px;
  min-height: 52px;
}

.topbar__title { flex: 1; font-size: 26px; font-weight: 600; margin: 0; letter-spacing: .02em; }

.topbar__search { flex: 1; min-width: 0; display: flex; align-items: center; gap: 6px; }
.field--search { flex: 1 1 auto; min-width: 0; padding: 9px 12px; }
/* flexの中では既定で縮むため、ボタンの文字が折り返してしまう */
.topbar__search .btn--plain { flex: 0 0 auto; white-space: nowrap; padding: 8px 4px; }

.topbar__actions { display: flex; }

.chips {
  display: flex;
  gap: 8px;
  overflow-x: auto;
  padding: 2px 16px 10px;
  scrollbar-width: none;
}
.chips::-webkit-scrollbar { display: none; }

.chip {
  flex: 0 0 auto;
  padding: 7px 14px;
  min-height: 34px;
  border: 1px solid transparent;
  border-radius: 999px;
  background: var(--fill);
  color: var(--ink-2);
  font-size: 13px;
  white-space: nowrap;
}
.chip[aria-selected="true"] { background: var(--ink); color: #fff; }
.chip--edit { background: none; border-color: var(--line); color: var(--ink-2); }

/* ---------- 一覧 ---------- */

.grid {
  display: grid;
  gap: 18px 12px;
  padding: 16px 16px calc(96px + var(--safe-b));
  grid-template-columns: repeat(auto-fill, minmax(132px, 1fr));
}

@media (min-width: 768px) {
  .grid {
    gap: 24px 16px;
    padding: 24px 24px calc(120px + var(--safe-b));
    grid-template-columns: repeat(auto-fill, minmax(150px, 1fr));
  }
}

.card {
  position: relative;
  display: block;
  width: 100%;
  padding: 0;
  border: none;
  background: none;
  text-align: left;
  color: inherit;
}

.card__cover {
  position: relative;
  aspect-ratio: 1 / 1.414;     /* A4比。説明書の判型に合わせる */
  background: var(--paper);
  border: 1px solid var(--line);
  border-radius: 6px;
  overflow: hidden;
  display: grid;
  place-items: center;
}
.card__cover img { width: 100%; height: 100%; object-fit: cover; display: block; }

.card__initial {
  font-size: 34px;
  font-weight: 300;
  color: var(--ink-3);
  user-select: none;
}

.card__more {
  position: absolute;
  top: 2px;
  right: 2px;
  width: 34px;
  height: 34px;
  display: grid;
  place-items: center;
  border: none;
  border-radius: 8px;
  background: rgba(255, 255, 255, .82);
  color: var(--ink-2);
  opacity: 0;
  transition: opacity .15s;
}
.card__cover:hover .card__more,
.card__more:focus-visible { opacity: 1; }
@media (hover: none) { .card__more { opacity: 1; } }
.card__more svg { width: 18px; height: 18px; }

.card__title {
  margin-top: 8px;
  font-size: 13px;
  font-weight: 500;
  line-height: 1.4;
  display: -webkit-box;
  -webkit-line-clamp: 2;
  -webkit-box-orient: vertical;
  overflow: hidden;
}

.card__meta { margin-top: 2px; font-size: 11px; color: var(--ink-2); }

.empty {
  grid-column: 1 / -1;
  padding: 72px 24px;
  text-align: center;
  color: var(--ink-2);
}
.empty__head { font-size: 17px; font-weight: 600; color: var(--ink); margin: 0 0 6px; }
.empty p { margin: 0; font-size: 14px; }

/* ---------- FAB ---------- */

.fab {
  position: fixed;
  right: 16px;
  bottom: calc(16px + var(--safe-b));
  z-index: 30;
  width: 56px;
  height: 56px;
  display: grid;
  place-items: center;
  border: none;
  border-radius: 28px;
  background: var(--ink);
  color: #fff;
  box-shadow: 0 4px 16px rgba(0, 0, 0, .18);
}
.fab svg { width: 26px; height: 26px; stroke-width: 1.9; }

/* ---------- シート ---------- */

.sheet { position: fixed; inset: 0; z-index: 60; }
.sheet__scrim { position: absolute; inset: 0; background: rgba(27, 27, 26, .35); }

.sheet__panel {
  position: absolute;
  left: 0; right: 0; bottom: 0;
  background: var(--paper);
  border-radius: 16px 16px 0 0;
  padding: 8px 16px calc(20px + var(--safe-b));
  max-height: 88vh;
  max-height: 88dvh;
  overflow-y: auto;
  animation: sheet-up .22s cubic-bezier(.2, .8, .3, 1);
}

@keyframes sheet-up { from { transform: translateY(16px); opacity: 0; } }

.sheet__grabber {
  width: 36px; height: 4px;
  margin: 0 auto 4px;
  border-radius: 2px;
  background: var(--line);
}

@media (min-width: 768px) {
  .sheet__panel {
    left: 50%; top: 50%; right: auto; bottom: 0;
    transform: translate(-50%, -50%);
    width: 460px;
    border-radius: 14px;
    max-height: 80vh;
    padding-bottom: 20px;
    animation: none;
  }
  .sheet__grabber { display: none; }
}

.sheet__title { font-size: 17px; font-weight: 600; margin: 6px 0 2px; }
.sheet__actions { display: grid; gap: 8px; margin-top: 20px; }
.sheet__divider { height: 1px; background: var(--line-2); margin: 20px -16px; border: 0; }

.optbtn {
  display: flex;
  align-items: center;
  gap: 12px;
  width: 100%;
  padding: 15px 14px;
  border: 1px solid var(--line);
  border-radius: var(--radius);
  background: var(--paper);
  text-align: left;
}
.optbtn__label { flex: 1; font-weight: 500; }
.optbtn__sub { display: block; font-size: 12px; font-weight: 400; color: var(--ink-2); }

.filelist { margin: 8px 0 0; display: grid; gap: 10px; }
.filelist__item { display: grid; gap: 6px; padding: 12px; background: var(--fill); border-radius: var(--radius); }
.filelist__name { font-size: 12px; color: var(--ink-2); overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }

.progress { margin-top: 14px; }
.progress__text { font-size: 13px; color: var(--ink-2); margin-bottom: 6px; }
.progress__track { height: 4px; background: var(--fill); border-radius: 2px; overflow: hidden; }
.progress__bar { height: 100%; width: 0; background: var(--ink); transition: width .18s; }

.catlist { display: grid; gap: 2px; margin-top: 8px; }
.catrow {
  display: flex;
  align-items: center;
  gap: 4px;
  padding: 6px 0;
  border-bottom: 1px solid var(--line-2);
}
.catrow__name {
  flex: 1;
  min-width: 0;
  padding: 8px 4px;
  border: 1px solid transparent;
  border-radius: 6px;
  background: none;
  font: inherit;
  color: var(--ink);
}
.catrow__name:focus { outline: none; background: var(--paper); border-color: var(--ink); }
.catrow .iconbtn { width: 38px; height: 38px; color: var(--ink-3); }
.catrow .iconbtn svg { width: 18px; height: 18px; }
.catrow__del { color: var(--danger) !important; }

.stat { display: flex; justify-content: space-between; align-items: baseline; padding: 10px 0; border-bottom: 1px solid var(--line-2); }
.stat__value { font-variant-numeric: tabular-nums; }

/* ---------- ビューア ---------- */

.viewer {
  position: fixed;
  inset: 0;
  z-index: 100;
  display: flex;
  flex-direction: column;
  background: var(--bg);
}

.viewer__bar {
  flex: 0 0 auto;
  display: flex;
  align-items: center;
  gap: 4px;
  padding: 6px 8px;
  padding-top: calc(6px + var(--safe-t));
  background: rgba(255, 255, 255, .92);
  backdrop-filter: saturate(1.6) blur(12px);
  -webkit-backdrop-filter: saturate(1.6) blur(12px);
  border-bottom: 1px solid var(--line);
}

.viewer__title {
  flex: 1;
  min-width: 0;
  font-size: 14px;
  font-weight: 500;
  text-align: center;
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}

.viewer__zoom { display: flex; }

.viewer__body {
  flex: 1 1 auto;
  overflow: auto;
  -webkit-overflow-scrolling: touch;
  padding: 12px 0 calc(24px + var(--safe-b));
}

.viewer__pages {
  margin: 0 auto;
  width: 100%;
  max-width: 900px;
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 12px;
}

.pdfpage {
  width: 100%;
  background: var(--paper);
  border: 1px solid var(--line);
}
.pdfpage canvas { width: 100%; height: auto; display: block; }

.viewer__image { width: 100%; height: auto; display: block; touch-action: pinch-zoom; }

.viewer__notice { padding: 48px 24px; text-align: center; color: var(--ink-2); }
.viewer__notice p { margin: 0 0 16px; font-size: 14px; }

.pageind {
  position: absolute;
  left: 50%;
  bottom: calc(20px + var(--safe-b));
  transform: translateX(-50%);
  padding: 6px 14px;
  border-radius: 999px;
  background: rgba(27, 27, 26, .72);
  color: #fff;
  font-size: 13px;
  font-variant-numeric: tabular-nums;
  pointer-events: none;
  transition: opacity .4s;
}
.pageind[data-fade="1"] { opacity: 0; }

/* ---------- トースト ---------- */

.toast {
  position: fixed;
  left: 50%;
  bottom: calc(88px + var(--safe-b));
  transform: translateX(-50%);
  z-index: 200;
  max-width: calc(100vw - 32px);
  padding: 11px 16px;
  border-radius: 10px;
  background: var(--ink);
  color: #fff;
  font-size: 14px;
  line-height: 1.45;
  box-shadow: 0 4px 16px rgba(0, 0, 0, .2);
}

@media (prefers-reduced-motion: reduce) {
  *, *::before, *::after {
    animation-duration: .01ms !important;
    animation-iteration-count: 1 !important;
    transition-duration: .01ms !important;
  }
}
