/* Kindle Assistant の見た目。ビルド不要の素の CSS。
   幅 375px（iPhone）を基準にし、広い画面では中央に 44rem まで。ダークモードは prefers-color-scheme で切り替える。 */

:root {
  color-scheme: light dark;
  --bg: #f7f5f0;
  --surface: #ffffff;
  --surface-2: #efece4;
  --text: #1f1d1a;
  --muted: #6b665d;
  --border: #d9d4c8;
  --accent: #8a4b14;
  --accent-text: #ffffff;
  --danger: #b3261e;
  --cite-bg: #fbe7b5;
  --cite-text: #5a3a00;
  --user-bg: #ebe6da;
  --focus: #1a5fb4;
  --radius: 10px;
  --tap: 44px;
  font-family: -apple-system, BlinkMacSystemFont, 'Hiragino Sans', 'Noto Sans JP', system-ui, sans-serif;
  font-size: 16px; /* iOS で入力欄にフォーカスしたとき拡大されないよう 16px 以上 */
  line-height: 1.7;
}

@media (prefers-color-scheme: dark) {
  :root {
    --bg: #16181c;
    --surface: #1f2228;
    --surface-2: #2a2e35;
    --text: #e8e6e1;
    --muted: #a19d94;
    --border: #3a3f47;
    --accent: #e0a060;
    --accent-text: #1a1206;
    --danger: #ff8a80;
    --cite-bg: #4a3a12;
    --cite-text: #ffe2a0;
    --user-bg: #2c3038;
    --focus: #8ab4f8;
  }
}

*,
*::before,
*::after {
  box-sizing: border-box;
}

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

body {
  margin: 0;
  background: var(--bg);
  color: var(--text);
  overflow-wrap: anywhere; /* 長い URL や英単語で横にはみ出さない */
}

#app {
  max-width: 44rem;
  margin: 0 auto;
  padding: 0 16px calc(16px + env(safe-area-inset-bottom));
  min-height: 100dvh;
}

:focus-visible {
  outline: 3px solid var(--focus);
  outline-offset: 2px;
}
h1:focus,
main:focus {
  outline: none;
}

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

[hidden] {
  display: none !important;
}

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

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

button,
.button {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  min-height: var(--tap);
  padding: 0 14px;
  border: 1px solid var(--border);
  border-radius: var(--radius);
  background: var(--surface);
  cursor: pointer;
  line-height: 1.2;
  touch-action: manipulation;
}
button:disabled {
  opacity: 0.5;
  cursor: default;
}
button.primary {
  background: var(--accent);
  border-color: var(--accent);
  color: var(--accent-text);
  font-weight: 600;
}
button.danger {
  color: var(--danger);
}
button.link {
  min-height: auto;
  padding: 0 4px;
  border: 0;
  background: none;
  color: var(--accent);
  text-decoration: underline;
}
/* ファイル選択: input は見えなくし、フォーカスはラベル（ボタンの見た目）に出す */
input[type='file']:focus-visible + .button {
  outline: 3px solid var(--focus);
  outline-offset: 2px;
}

select,
input[type='text'],
input[type='search'],
input[type='password'],
textarea {
  width: 100%;
  min-height: var(--tap);
  padding: 8px 12px;
  border: 1px solid var(--border);
  border-radius: var(--radius);
  background: var(--surface);
}
textarea {
  resize: vertical;
  line-height: 1.6;
}

a {
  color: var(--accent);
}

.status,
.hint,
.meta,
.author,
.mode-hint {
  color: var(--muted);
  font-size: 0.875rem;
  margin: 4px 0;
}

.row {
  display: flex;
  flex-wrap: wrap;
  gap: 8px;
  align-items: center;
  margin: 8px 0;
}
.row input {
  flex: 1 1 12rem;
}

/* ---- ヘッダー・バナー ---- */

.topbar {
  position: sticky;
  top: 0;
  z-index: 10;
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 8px;
  margin: 0 -16px;
  padding: calc(6px + env(safe-area-inset-top)) 16px 6px;
  background: var(--bg);
  border-bottom: 1px solid var(--border);
}
.app-title {
  border: 0;
  background: none;
  padding: 0;
  font-weight: 700;
  letter-spacing: 0.02em;
}
.banner {
  display: flex;
  flex-wrap: wrap;
  align-items: center;
  gap: 8px;
  margin: 12px 0;
  padding: 10px 12px;
  border: 1px solid var(--danger);
  border-radius: var(--radius);
  background: var(--surface);
}
.banner span {
  flex: 1 1 14rem;
}

h1 {
  font-size: 1.375rem;
  line-height: 1.4;
  margin: 16px 0 4px;
}
h2 {
  font-size: 1.0625rem;
  margin: 0 0 8px;
}

/* ---- 本の一覧 ---- */

.books-view .primary {
  margin: 8px 0;
}
.search {
  margin: 8px 0;
}
.book-list {
  list-style: none;
  padding: 0;
  margin: 8px 0;
  display: grid;
  gap: 8px;
}
.book {
  width: 100%;
  min-height: 56px;
  flex-direction: column;
  align-items: flex-start;
  padding: 10px 14px;
  text-align: left;
  gap: 2px;
}
.book-title {
  font-weight: 600;
  line-height: 1.45;
}
.book-meta {
  color: var(--muted);
  font-size: 0.8125rem;
}
.empty {
  color: var(--muted);
  padding: 12px 0;
  list-style: none;
}

/* ---- 質問画面 ---- */

.chat-head .back {
  margin-top: 12px;
  border: 0;
  background: none;
  padding: 0 4px 0 0;
  color: var(--accent);
}
.conv-bar {
  display: flex;
  flex-wrap: wrap;
  gap: 8px;
  align-items: flex-start;
  margin: 8px 0;
}
.conv-select {
  flex: 1 1 100%;
}
.more {
  position: relative;
}
.more summary {
  display: inline-flex;
  align-items: center;
  min-height: var(--tap);
  padding: 0 14px;
  border: 1px solid var(--border);
  border-radius: var(--radius);
  background: var(--surface);
  cursor: pointer;
  list-style: none;
}
.more summary::-webkit-details-marker {
  display: none;
}
.more-body {
  display: flex;
  flex-wrap: wrap;
  gap: 8px;
  margin-top: 8px;
}

.messages {
  list-style: none;
  padding: 0;
  margin: 12px 0;
  display: grid;
  gap: 12px;
}
.msg {
  padding: 10px 14px;
  border-radius: var(--radius);
  min-width: 0;
}
.msg.user {
  background: var(--user-bg);
  margin-left: 12%;
}
.msg-text {
  margin: 0;
  white-space: pre-wrap;
}
.msg.assistant {
  background: var(--surface);
  border: 1px solid var(--border);
}
.msg-status {
  color: var(--muted);
  font-size: 0.875rem;
  margin: 6px 0 0;
}
.msg-status.error {
  color: var(--danger);
}
.tag {
  display: inline-block;
  margin-top: 4px;
  padding: 0 8px;
  border-radius: 999px;
  background: var(--surface);
  color: var(--muted);
  font-size: 0.75rem;
}
/* ネタバレ防止（#21）とハイライト（#23） */
.book-extras {
  display: grid;
  gap: 8px;
  margin: 8px 0;
}
.spoiler {
  padding: 8px 12px;
  border: 1px solid var(--border);
  border-radius: var(--radius);
  background: var(--surface);
}
.spoiler .hint {
  margin: 2px 0 0;
}
/* 意味検索（埋め込み #22） */
.embed-panel {
  margin: 8px 0;
  padding: 8px 12px;
  border: 1px solid var(--border);
  border-radius: var(--radius);
  background: var(--surface);
}
.embed-panel .hint {
  margin: 2px 0;
}
.embed-panel .hint.error {
  color: var(--danger);
}
.embed-panel progress {
  width: 100%;
  accent-color: var(--accent);
}
.embed-panel .row {
  margin-top: 4px;
}
.semantic-consent {
  margin: 10px 0 0;
}
label.switch,
.card label.switch {
  display: flex;
  align-items: center;
  gap: 8px;
  min-height: var(--tap);
  margin: 0;
  cursor: pointer;
}
label.switch input[type='checkbox'] {
  width: 22px;
  height: 22px;
  margin: 0;
  accent-color: var(--accent);
}
.spoiler-seq {
  display: flex;
  flex-wrap: wrap;
  align-items: center;
  gap: 8px;
}
.seq-number {
  width: 5.5rem;
  min-height: var(--tap);
  padding: 4px 8px;
  border: 1px solid var(--border);
  border-radius: var(--radius);
  background: var(--surface);
}
.seq-range {
  flex: 1 1 10rem;
  min-height: var(--tap);
  accent-color: var(--accent);
}
.highlights summary {
  display: flex;
  align-items: center;
  min-height: var(--tap);
  padding: 0 12px;
  border: 1px solid var(--border);
  border-radius: var(--radius);
  background: var(--surface);
  cursor: pointer;
}
.highlights[open] summary {
  border-bottom-left-radius: 0;
  border-bottom-right-radius: 0;
}
.highlight-list {
  list-style: none;
  margin: 0;
  padding: 0;
  max-height: 50vh;
  overflow-y: auto;
  border: 1px solid var(--border);
  border-top: 0;
  border-radius: 0 0 var(--radius) var(--radius);
  background: var(--surface);
}
.highlight {
  padding: 8px 12px;
  border-top: 1px solid var(--border);
}
.highlight:first-child {
  border-top: 0;
}
.highlight-meta {
  margin: 0;
  color: var(--muted);
  font-size: 0.8125rem;
}
.highlight .tag {
  margin: 0;
  background: var(--surface-2);
}
.highlight.kind-highlight .tag {
  background: var(--cite-bg);
  color: var(--cite-text);
}
.highlight-text {
  margin: 2px 0 0;
  white-space: pre-wrap;
  overflow-wrap: anywhere;
}
.highlight-text.empty-text {
  color: var(--muted);
}
.msg.assistant[aria-busy='true'] .md::after {
  content: '▍';
  animation: blink 1s steps(2) infinite;
}
@keyframes blink {
  to {
    visibility: hidden;
  }
}
@media (prefers-reduced-motion: reduce) {
  .msg.assistant[aria-busy='true'] .md::after {
    animation: none;
  }
}

/* Markdown */
.md > :first-child {
  margin-top: 0;
}
.md > :last-child {
  margin-bottom: 0;
}
.md p,
.md ul,
.md ol,
.md blockquote,
.md pre {
  margin: 0.5em 0;
}
.md ul,
.md ol {
  padding-left: 1.4em;
}
.md h3,
.md h4,
.md h5,
.md h6 {
  font-size: 1rem;
  margin: 0.9em 0 0.3em;
}
.md blockquote {
  padding-left: 0.8em;
  border-left: 3px solid var(--border);
  color: var(--muted);
}
.md code {
  font-family: ui-monospace, SFMono-Regular, Menlo, monospace;
  font-size: 0.875em;
  background: var(--surface-2);
  padding: 0.1em 0.3em;
  border-radius: 4px;
}
.md pre {
  overflow-x: auto;
  padding: 10px;
  background: var(--surface-2);
  border-radius: 6px;
}
.md pre code {
  background: none;
  padding: 0;
}
.md .table-wrap {
  overflow-x: auto;
}
.md table {
  border-collapse: collapse;
  font-size: 0.875rem;
}
.md th,
.md td {
  border: 1px solid var(--border);
  padding: 4px 8px;
  text-align: left;
  overflow-wrap: normal;
}
.md hr {
  border: 0;
  border-top: 1px solid var(--border);
}
mark.cite {
  background: var(--cite-bg);
  color: var(--cite-text);
  padding: 0 0.25em;
  border-radius: 4px;
  font-size: 0.875em;
  font-weight: 600;
  white-space: nowrap;
}

.notice {
  margin: 12px 0;
  padding: 10px 14px;
  border: 1px solid var(--accent);
  border-radius: var(--radius);
  background: var(--surface);
}
.notice p {
  margin: 0 0 8px;
}
.notice p:last-child {
  margin: 0;
}
.notice-actions {
  display: flex;
  flex-wrap: wrap;
  gap: 8px;
}

.composer {
  position: sticky;
  bottom: 0;
  margin: 0 -16px;
  padding: 8px 16px calc(8px + env(safe-area-inset-bottom));
  background: var(--bg);
  border-top: 1px solid var(--border);
}
.composer-actions {
  display: flex;
  flex-wrap: wrap;
  align-items: center;
  gap: 8px;
  margin-top: 8px;
}
.composer-actions .spacer {
  flex: 1 1 auto;
}
.attach-preview {
  display: flex;
  align-items: center;
  gap: 8px;
  margin-bottom: 8px;
}
.attach-preview img {
  width: 48px;
  height: 64px;
  object-fit: cover;
  border-radius: 6px;
  border: 1px solid var(--border);
}
.preview-info {
  flex: 1;
  color: var(--muted);
  font-size: 0.8125rem;
}
.mode-hint {
  display: block;
}

/* ---- 設定 ---- */

.card {
  margin: 12px 0;
  padding: 14px;
  border: 1px solid var(--border);
  border-radius: var(--radius);
  background: var(--surface);
}
.card label {
  display: block;
  margin: 10px 0 4px;
  font-weight: 600;
  font-size: 0.9375rem;
}
.card select + input,
.card select + .hint {
  margin-top: 8px;
}
.key-form {
  padding: 8px 0;
  border-top: 1px solid var(--border);
}
.key-form:first-child {
  border-top: 0;
}
.key-form.active label::after {
  content: '（使用中）';
  color: var(--accent);
  font-weight: 400;
}
