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

:root {
  --bg: #faf9f7;
  --surface: #fff;
  --border: #e0ddd8;
  --text: #2c2a26;
  --text-sub: #6b6760;
  --accent: #6b3a5d;
  --accent-light: #ecdfe8;
  --accent-hover: #4d2843;
  --tag-noun: #3566a0;
  --tag-propn: #7a4da0;
  --tag-verb: #a05035;
  --tag-adj: #0e7c6b;
  --tag-adv: #5a6ea0;
  --tag-det: #8a7030;
}

body {
  font-family: "Noto Sans JP", "Hiragino Sans", "Yu Gothic", sans-serif;
  background: var(--bg);
  color: var(--text);
  line-height: 1.7;
  min-height: 100vh;
}

body.page-index,
body.page-kwic {
  height: 100vh;
  overflow: hidden;
  display: flex;
  flex-direction: column;
}

header {
  background: var(--accent);
  color: #fff;
  padding: 0.5rem 1rem 0.8rem;
  flex-shrink: 0;
}

header h1 {
  font-size: 1.5rem;
  font-weight: 600;
  letter-spacing: 0.1em;
}

.subtitle {
  margin-top: 0.3rem;
  font-size: 0.9rem;
  opacity: 0.85;
}

main {
  max-width: 1000px;
  width: 100%;
  margin: 0 auto;
  padding: 1.5rem 1.5rem 3rem;
}

.page-index main,
.page-kwic main {
  padding: 1rem 1.5rem;
  flex: 1;
  display: flex;
  flex-direction: column;
  overflow: hidden;
}

/* --- controls --- */
.controls {
  display: flex;
  flex-wrap: wrap;
  gap: 1.2rem;
  align-items: flex-end;
  margin-bottom: 0.8rem;
  padding: 1rem 1.2rem;
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: 6px;
  flex-shrink: 0;
}

.control-group {
  display: flex;
  align-items: center;
  gap: 0.5rem;
}

.control-group label {
  font-size: 0.82rem;
  color: var(--text-sub);
  letter-spacing: 0.05em;
  white-space: nowrap;
}

.pos-filters {
  display: flex;
  gap: 4px;
  flex-wrap: wrap;
}

.pos-btn {
  font-family: inherit;
  font-size: 0.82rem;
  padding: 0.3rem 0.7rem;
  border: 1px solid var(--border);
  border-radius: 4px;
  background: var(--surface);
  color: var(--text);
  cursor: pointer;
  transition: all 0.15s;
}

.pos-btn:hover {
  border-color: var(--accent);
  color: var(--accent);
}

.pos-btn.active {
  background: var(--accent);
  color: #fff;
  border-color: var(--accent);
}

select,
input[type="text"] {
  font-family: inherit;
  font-size: 0.88rem;
  padding: 0.35rem 0.6rem;
  border: 1px solid var(--border);
  border-radius: 4px;
  background: var(--surface);
  color: var(--text);
}

input[type="text"] {
  width: 180px;
}

input[type="text"]:focus,
select:focus {
  outline: none;
  border-color: var(--accent);
  box-shadow: 0 0 0 2px var(--accent-light);
}

/* --- result info --- */
.result-info {
  font-size: 0.82rem;
  color: var(--text-sub);
  padding-left: 0.2rem;
  flex-shrink: 0;
}

/* --- table wrapper --- */
.table-wrapper {
  flex: 1;
  overflow-y: auto;
  border: 1px solid var(--border);
  border-radius: 6px;
  background: var(--surface);
}

/* --- table --- */
.word-table {
  width: 100%;
  border-collapse: collapse;
  font-size: 0.9rem;
}

.word-table thead {
  background: #f5f3f0;
  position: sticky;
  top: 0;
  z-index: 2;
}

.word-table th {
  padding: 0.6rem 0.7rem;
  text-align: left;
  font-weight: 600;
  font-size: 0.78rem;
  color: var(--text-sub);
  letter-spacing: 0.04em;
  border-bottom: 2px solid var(--border);
  white-space: nowrap;
  background: #f5f3f0;
}

.word-table td {
  padding: 0.5rem 0.7rem;
  border-bottom: 1px solid #eeece8;
  vertical-align: top;
}

.word-table tbody tr:hover {
  background: #faf8f5;
}

.col-lemma { width: 12%; }
.col-reading { width: 12%; }
.col-pos { width: 7%; }
.col-xpos { width: 18%; }
.col-count { width: 8%; text-align: right; }
.col-haiku { width: 8%; text-align: right; }
.col-forms { width: 35%; }

.word-table td:nth-child(5),
.word-table td:nth-child(6) {
  text-align: right;
  font-variant-numeric: tabular-nums;
}

td.lemma-cell {
  font-weight: 600;
  font-size: 1rem;
}

.pos-tag {
  display: inline-block;
  font-size: 0.72rem;
  padding: 0.1rem 0.45rem;
  border-radius: 3px;
  color: #fff;
  white-space: nowrap;
}

.pos-tag[data-pos="NOUN"]  { background: var(--tag-noun); }
.pos-tag[data-pos="PROPN"] { background: var(--tag-propn); }
.pos-tag[data-pos="VERB"]  { background: var(--tag-verb); }
.pos-tag[data-pos="ADJ"]   { background: var(--tag-adj); }
.pos-tag[data-pos="ADV"]   { background: var(--tag-adv); }
.pos-tag[data-pos="DET"]   { background: var(--tag-det); }

.forms-cell {
  font-size: 0.82rem;
  color: var(--text-sub);
  line-height: 1.5;
}

.xpos-cell {
  font-size: 0.8rem;
  color: var(--text-sub);
}

/* --- pagination --- */
.pagination {
  display: flex;
  justify-content: center;
  align-items: center;
  gap: 4px;
  padding-top: 0.8rem;
  flex-wrap: wrap;
  flex-shrink: 0;
}

.page-btn {
  font-family: inherit;
  font-size: 0.85rem;
  min-width: 2.2rem;
  padding: 0.35rem 0.55rem;
  border: 1px solid var(--border);
  border-radius: 4px;
  background: var(--surface);
  color: var(--text);
  cursor: pointer;
  transition: all 0.15s;
  text-align: center;
}

.page-btn:hover {
  border-color: var(--accent);
  color: var(--accent);
}

.page-btn.active {
  background: var(--accent);
  color: #fff;
  border-color: var(--accent);
  font-weight: 600;
}

.page-btn:disabled {
  opacity: 0.4;
  cursor: default;
}

.page-ellipsis {
  padding: 0.35rem 0.3rem;
  color: var(--text-sub);
  font-size: 0.85rem;
}

/* --- word link in index table --- */
.word-link {
  color: var(--text);
  text-decoration: none;
  border-bottom: 1px solid transparent;
  transition: border-color 0.15s, color 0.15s;
}

.word-link:hover {
  color: var(--accent);
  border-bottom-color: var(--accent);
}

/* --- header link --- */
.header-link {
  color: inherit;
  text-decoration: none;
}

.header-link:hover {
  opacity: 0.85;
}

/* --- word detail page --- */
.word-header {
  margin-bottom: 1.2rem;
  flex-shrink: 0;
}

.back-link {
  display: inline-block;
  font-size: 0.85rem;
  color: var(--accent);
  text-decoration: none;
  margin-bottom: 0.8rem;
}

header .back-link {
  color: rgba(255,255,255,0.85);
  margin-bottom: 0;
  margin-left: 0.5rem;
}

header .back-link:hover {
  color: #fff;
}

.back-link:hover {
  text-decoration: underline;
}

.word-info {
  display: flex;
  align-items: baseline;
  gap: 0.7rem;
  flex-wrap: wrap;
}

.word-lemma {
  font-size: 1.6rem;
  font-weight: 700;
}

.word-reading {
  font-size: 1rem;
  color: var(--text-sub);
}

.word-count {
  font-size: 0.88rem;
  color: var(--text-sub);
  font-variant-numeric: tabular-nums;
}

/* --- KWIC section flex --- */
#kwic-section {
  display: flex;
  flex-direction: column;
  flex: 1;
  overflow: hidden;
}

/* --- view toggle --- */
.view-toggle {
  display: flex;
  gap: 4px;
  margin-bottom: 0.8rem;
  flex-shrink: 0;
}

.view-btn {
  font-family: inherit;
  font-size: 0.85rem;
  padding: 0.35rem 1rem;
  border: 1px solid var(--border);
  border-radius: 4px;
  background: var(--surface);
  color: var(--text);
  cursor: pointer;
  transition: all 0.15s;
}

.view-btn:hover {
  border-color: var(--accent);
  color: var(--accent);
}

.view-btn.active {
  background: var(--accent);
  color: #fff;
  border-color: var(--accent);
}

/* --- kwic controls --- */
.kwic-controls {
  display: flex;
  align-items: center;
  gap: 1.2rem;
  margin-bottom: 0.8rem;
  padding: 0.5rem 1.2rem;
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: 6px;
  flex-shrink: 0;
}

/* --- tag cloud --- */
.tag-cloud {
  display: flex;
  flex-wrap: wrap;
  align-items: baseline;
  gap: 0.3rem 0.6rem;
  padding: 0.6rem 1rem;
  margin-bottom: 0.8rem;
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: 6px;
  flex-shrink: 0;
  max-height: 10rem;
  overflow-y: auto;
}

.tag-cloud:empty {
  display: none;
}

.tag-cloud-item {
  text-decoration: none;
  cursor: pointer;
  line-height: 1.3;
  transition: opacity 0.15s;
}

.tag-cloud-item:hover {
  opacity: 0.7;
}

.tag-cloud-item[data-pos="NOUN"]  { color: var(--tag-noun); }
.tag-cloud-item[data-pos="PROPN"] { color: var(--tag-propn); }
.tag-cloud-item[data-pos="VERB"]  { color: var(--tag-verb); }
.tag-cloud-item[data-pos="ADJ"]   { color: var(--tag-adj); }
.tag-cloud-item[data-pos="ADV"]   { color: var(--tag-adv); }
.tag-cloud-item[data-pos="DET"]   { color: var(--tag-det); }

/* --- co-occurrence filter --- */
.cooc-filter {
  display: flex;
  align-items: center;
  gap: 0.5rem;
  padding: 0.4rem 1rem;
  margin-bottom: 0.6rem;
  background: var(--accent-light);
  border: 1px solid var(--accent);
  border-radius: 6px;
  font-size: 0.85rem;
  flex-shrink: 0;
}

.cooc-filter-label {
  color: var(--text-sub);
}

.cooc-filter-word {
  font-weight: 700;
  color: var(--accent);
}

.cooc-filter-clear {
  font-family: inherit;
  font-size: 0.78rem;
  padding: 0.15rem 0.5rem;
  border: 1px solid var(--accent);
  border-radius: 3px;
  background: var(--surface);
  color: var(--accent);
  cursor: pointer;
  margin-left: auto;
}

.cooc-filter-clear:hover {
  background: var(--accent);
  color: #fff;
}

.kwic-cooc {
  color: #1e5cb8 !important;
  font-weight: 700;
}

.tag-cloud-item.active {
  outline: 2px solid var(--text);
  outline-offset: 1px;
  border-radius: 2px;
}

/* --- kwic table --- */
.kwic-container {
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: 6px;
  overflow-y: auto;
  padding: 0.5rem 0;
  flex: 1;
}

.kwic-table {
  width: 100%;
  border-collapse: collapse;
  table-layout: fixed;
}

.kwic-col-left  { width: 45%; }
.kwic-col-right { width: 45%; }
.kwic-col-coll  { width: 10%; }

.kwic-table td {
  padding: 0.2rem 0;
  line-height: 1.9;
  font-size: 0.95rem;
  vertical-align: baseline;
  border-bottom: 1px solid #f0eeea;
}

.kwic-table tr:hover td {
  background: #faf8f5;
}

td.kwic-left {
  text-align: right;
  padding-right: 0;
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}

td.kwic-right {
  text-align: left;
  padding-left: 0;
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}

.kwic-kw {
  font-weight: 700;
  color: #c03030;
}

.kwic-ctx-link {
  color: var(--text);
  text-decoration: none;
  cursor: pointer;
}

.kwic-ctx-link:hover {
  color: var(--accent);
  background: #c0d0e8;
  border-radius: 2px;
}

#kwic-tip {
  display: none;
  position: fixed;
  white-space: nowrap;
  background: var(--text);
  color: #fff;
  font-size: 0.72rem;
  font-weight: 400;
  padding: 0.2rem 0.5rem;
  border-radius: 3px;
  pointer-events: none;
  z-index: 1000;
}

td.kwic-coll {
  text-align: right;
  padding-right: 1rem;
  color: var(--text-sub);
  font-size: 0.82rem;
  white-space: nowrap;
}

/* --- collocation view --- */
#coll-section {
  display: flex;
  flex-direction: column;
  flex: 1;
  overflow: hidden;
}

.coll-controls {
  display: flex;
  flex-direction: column;
  gap: 0.4rem;
  padding: 0.5rem 1rem;
  margin-bottom: 0.8rem;
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: 6px;
  flex-shrink: 0;
}

.coll-row-group {
  display: flex;
  align-items: center;
  gap: 0.5rem;
}

.coll-row-label {
  font-size: 0.78rem;
  font-weight: 600;
  color: var(--text-sub);
  min-width: 3.5em;
  flex-shrink: 0;
}

.coll-row-inactive {
  opacity: 0.45;
}

.coll-btns {
  display: flex;
  gap: 4px;
  flex-wrap: wrap;
}

.case-btn {
  font-family: inherit;
  font-size: 0.82rem;
  padding: 0.25rem 0.6rem;
  border: 1px solid var(--border);
  border-radius: 4px;
  background: var(--surface);
  color: var(--text);
  cursor: pointer;
  transition: all 0.15s;
}

.case-btn:hover {
  border-color: var(--accent);
  color: var(--accent);
}

.case-btn.active {
  background: var(--accent);
  color: #fff;
  border-color: var(--accent);
}

.case-count {
  font-size: 0.72rem;
  opacity: 0.7;
  margin-left: 0.2rem;
}

.case-count::before { content: "("; }
.case-count::after  { content: ")"; }

.coll-container {
  flex: 1;
  overflow-y: auto;
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: 6px;
}

.coll-table {
  width: 100%;
  border-collapse: collapse;
}

.coll-table thead {
  position: sticky;
  top: 0;
  z-index: 2;
  background: #f5f3f0;
}

.coll-table th {
  padding: 0.5rem 0.7rem;
  text-align: left;
  font-weight: 600;
  font-size: 0.78rem;
  color: var(--text-sub);
  letter-spacing: 0.04em;
  border-bottom: 2px solid var(--border);
  background: #f5f3f0;
}

.coll-col-word  { width: 10%; }
.coll-col-haiku { width: 78%; }
.coll-col-coll  { width: 12%; }

.coll-table td {
  padding: 0.25rem 0.7rem;
  border-bottom: 1px solid #f0eeea;
  vertical-align: baseline;
  font-size: 0.92rem;
  line-height: 1.8;
}

.coll-table tr:hover td {
  background: #faf8f5;
}

.coll-case-header td {
  font-weight: 700;
  font-size: 0.85rem;
  padding: 0.6rem 0.7rem 0.3rem;
  color: var(--accent);
  border-bottom: 2px solid var(--accent-light);
  background: #f8f6f3;
}

.coll-row.coll-first td {
  border-top: 1px solid var(--border);
}

.coll-word-cell {
  font-weight: 600;
  white-space: nowrap;
}

.coll-word-cell a {
  color: var(--text);
  text-decoration: none;
}

.coll-word-cell a:hover {
  color: var(--accent);
}

.coll-verb-count {
  font-size: 0.72rem;
  color: var(--text-sub);
  font-weight: 400;
  margin-left: 0.25rem;
}

td.coll-coll {
  text-align: right;
  padding-right: 1rem;
  color: var(--text-sub);
  font-size: 0.82rem;
  white-space: nowrap;
}

.coll-hl-noun {
  color: #c03030;
  font-weight: 700;
}

.coll-hl-verb {
  color: #1e5cb8;
  font-weight: 700;
}

.coll-hl-adj {
  color: #0e7c6b;
  font-weight: 700;
}

.coll-hl-nn-mod {
  color: var(--tag-noun);
  font-weight: 700;
}

.coll-hl-nn-head {
  color: #c03030;
  font-weight: 700;
}

/* --- responsive --- */
@media (max-width: 700px) {
  header { padding: 1.2rem 1rem 1rem; }
  main { padding: 1rem; }
  .controls { flex-direction: column; align-items: stretch; }
  .kwic-controls { flex-direction: column; align-items: stretch; }
  input[type="text"] { width: 100%; }
  .word-table { font-size: 0.82rem; }
  .col-xpos, .col-forms { display: none; }
  .kwic-col-left  { width: 42%; }
  .kwic-col-right { width: 42%; }
  .kwic-col-coll  { width: 16%; }
  .kwic-table td { font-size: 0.82rem; }
}
