:root {
  --bg: #efefef;
  --fg: #111;
  --sub: #666;
  --reply: #f7f7f7;
  --line: #d6d6d6;
  --quote: #2b6f9e;
  --link: #0645ad;
  --no: #228b22;
  --title: #c00;
  --side: #f6f6f6;
  --sel: #dde6f3;
  --mark: #ffe066;
  --pop: #fbfbf4;
  --shadow: rgba(0, 0, 0, .25);
  --head: #e2e2e2;
  --new: #d60;
}
@media (prefers-color-scheme: dark) {
  :root:not([data-theme="light"]) {
    --bg: #18191b; --fg: #ddd; --sub: #999; --reply: #222326; --line: #3a3b3f;
    --quote: #7cb7e0; --link: #8fb3ff; --no: #6fcf97; --title: #ff7a6b; --side: #1d1e21;
    --sel: #2c3a4f; --mark: #8a6d00; --pop: #26272b; --shadow: rgba(0,0,0,.6); --head: #26272b; --new: #ffa35c;
  }
}
:root[data-theme="dark"] {
  --bg: #18191b; --fg: #ddd; --sub: #999; --reply: #222326; --line: #3a3b3f;
  --quote: #7cb7e0; --link: #8fb3ff; --no: #6fcf97; --title: #ff7a6b; --side: #1d1e21;
  --sel: #2c3a4f; --mark: #8a6d00; --pop: #26272b; --shadow: rgba(0,0,0,.6); --head: #26272b; --new: #ffa35c;
}

* { box-sizing: border-box; }
html, body { height: 100%; margin: 0; }
body {
  background: var(--bg); color: var(--fg);
  font: 14px/1.6 "Hiragino Kaku Gothic ProN", "Yu Gothic UI", "Meiryo", sans-serif;
  display: flex; flex-direction: column;
}
a { color: var(--link); }
button, select, input { font: inherit; color: inherit; }
button, select {
  background: var(--reply); border: 1px solid var(--line); border-radius: 4px; padding: 3px 9px; cursor: pointer;
}
button:hover { filter: brightness(.95); }
mark { background: var(--mark); color: inherit; border-radius: 2px; }
mark.cur { outline: 2px solid var(--title); }

/* header */
#top {
  display: flex; gap: 8px; align-items: center; flex-wrap: wrap;
  padding: 6px 12px; background: var(--head); border-bottom: 1px solid var(--line);
  position: relative; z-index: 10; transition: margin-top .2s;
}
#home { font-weight: bold; color: var(--title); text-decoration: none; font-size: 16px; white-space: nowrap; }
#searchForm { display: flex; gap: 4px; flex: 1 1 300px; min-width: 200px; }
#q {
  flex: 1; padding: 4px 8px; border: 1px solid var(--line); border-radius: 4px; background: var(--bg);
}
#status { font-size: 12px; color: var(--sub); }
#refreshBtn:disabled { opacity: .6; cursor: default; }

#wrap { flex: 1; display: flex; min-height: 0; }
#side {
  width: 360px; flex-shrink: 0; overflow-y: auto; background: var(--side); border-right: 1px solid var(--line);
}
#sideTop { position: sticky; top: 0; background: var(--side); z-index: 1; transition: transform .2s;
  border-bottom: 1px solid var(--line); }
#sideHead { padding: 6px 10px; font-size: 12px; color: var(--sub); }
#sideHead .bt { font-weight: bold; color: var(--title); }
body.hidebars #sideTop { transform: translateY(-100%); }
#cats { display: flex; border-bottom: 1px solid var(--line); }
#cats button { flex: 1; border: 0; border-radius: 0; background: transparent; padding: 7px 4px; font-size: 13px;
  color: var(--sub); border-bottom: 3px solid transparent; cursor: pointer; white-space: nowrap; }
#cats button.on { color: var(--title); font-weight: bold; border-bottom-color: var(--title); }
#cats button small { font-weight: normal; margin-left: 3px; }
#cats .unread { margin-left: 3px; }
#view { flex: 1; overflow-y: auto; min-width: 0; position: relative; }
#empty { padding: 40px; color: var(--sub); }

/* thread list */
.ti { padding: 6px 10px; border-bottom: 1px solid var(--line); cursor: pointer; }
.ti:hover { background: var(--reply); }
.ti.sel { background: var(--sel); }
.ti .tt { font-weight: bold; color: var(--title); word-break: break-all;
  display: -webkit-box; -webkit-line-clamp: 2; -webkit-box-orient: vertical; overflow: hidden; }
.ti small { color: var(--sub); }
.ti .unread { color: var(--new); font-weight: bold; margin-left: 4px; }
.ti .arc, .ti .full { font-size: 11px; color: var(--sub); border: 1px solid var(--line); border-radius: 3px; padding: 0 4px; margin-left: 4px; }
.ti .pend { color: var(--sub); margin-left: 4px; }

/* search results */
.sr-thread { border-bottom: 1px solid var(--line); }
.sr-head { padding: 6px 10px 2px; cursor: pointer; }
.sr-head b { color: var(--title); }
.sr-head small { color: var(--sub); margin-left: 6px; }
.sr-hit { padding: 3px 10px 3px 20px; cursor: pointer; font-size: 13px; word-break: break-all; }
.sr-hit:hover { background: var(--reply); }
.sr-hit .n { color: var(--no); margin-right: 6px; }
.sr-more { padding: 3px 20px 6px; font-size: 12px; color: var(--link); cursor: pointer; }
.sr-hit .sd { color: var(--link); font-size: 11px; margin-right: 6px; }
.sr-sec { padding: 4px 10px; font-size: 12px; font-weight: bold; color: var(--sub); background: var(--head); border-bottom: 1px solid var(--line); }
.sr-sum { color: var(--sub); font-size: 11px; }
.sr-err { color: #d00; font-weight: bold; }
.sr-partial { color: var(--new); }

/* search options */
#soBtn.on { background: var(--sel); font-weight: bold; }
#orBtn.on { background: var(--title); border-color: var(--title); color: var(--bg); font-weight: bold; }
#sopts {
  padding: 8px 12px; background: var(--side); border-bottom: 1px solid var(--line); font-size: 13px;
  display: flex; flex-direction: column; gap: 6px; max-height: 60vh; overflow-y: auto;
}
#sopts[hidden] { display: none; }
.so-row { display: flex; flex-wrap: wrap; gap: 4px 14px; align-items: center; }
.so-lb { font-weight: bold; color: var(--title); min-width: 2.5em; }
.so-row label { display: inline-flex; align-items: center; gap: 3px; cursor: pointer; }
.so-row input[type=date] {
  padding: 2px 4px; border: 1px solid var(--line); border-radius: 4px; background: var(--bg); color: inherit;
}
.so-help summary { cursor: pointer; color: var(--sub); }
.so-help table { border-collapse: collapse; margin-top: 4px; font-size: 12px; }
.so-help td { padding: 2px 10px 2px 0; vertical-align: top; }
.so-help code { background: var(--reply); padding: 0 4px; border-radius: 3px; white-space: nowrap; }

/* bookmarks */
.ph .bmk { cursor: pointer; color: var(--sub); font-size: 15px; line-height: 1; user-select: none; }
.ph .bmk.on { color: #e6a700; }
.ph .bmk:hover { transform: scale(1.2); }
#bmBtn.on { background: var(--sel); font-weight: bold; }
.bm-tools { display: flex; gap: 6px; align-items: center; flex-wrap: wrap; }
.bm { padding: 6px 10px; border-bottom: 1px solid var(--line); cursor: pointer; position: relative; }
.bm:hover { background: var(--reply); }
.bm.dragging { opacity: .4; }
.bm.over { box-shadow: inset 0 3px 0 var(--title); }
.bm-name { font-weight: bold; color: var(--title); word-break: break-all; }
.bm-name .unnamed { color: var(--sub); font-weight: normal; }
.bm-sub { font-size: 12px; color: var(--sub); word-break: break-all; }
.bm-body { font-size: 12px; white-space: pre-wrap; word-break: break-all; margin-top: 2px;
  display: -webkit-box; -webkit-line-clamp: 3; -webkit-box-orient: vertical; overflow: hidden; }
.bm-ops { display: flex; gap: 4px; margin-top: 4px; }
.bm-ops button { padding: 1px 8px; font-size: 12px; }
.bm-ops .grip { cursor: grab; color: var(--sub); padding: 0 4px; user-select: none; }

/* thread toolbar */
#tbar {
  position: sticky; top: 0; z-index: 5; background: var(--bg); border-bottom: 1px solid var(--line);
  padding: 6px 14px; display: flex; flex-wrap: wrap; gap: 6px 10px; align-items: center;
  transition: transform .2s;
}
body.hidebars #tbar { transform: translateY(-100%); }
#tbar .ttl { font-weight: bold; color: var(--title); flex: 1 1 100%; word-break: break-all; cursor: pointer; }
.zoom { display: inline-flex; }
.zoom button { border-radius: 0; padding: 2px 7px; }
.zoom button:first-child { border-radius: 4px 0 0 4px; }
.zoom button:last-child { border-radius: 0 4px 4px 0; }
.zoom .zoomLbl { min-width: 3.4em; font-size: 12px; border-left: 0; border-right: 0; }
#tbar small { color: var(--sub); }
#tbar .grow { flex: 1; }
#tfind { width: 170px; padding: 2px 6px; border: 1px solid var(--line); border-radius: 4px; background: var(--bg); }
#tfindCount { font-size: 12px; color: var(--sub); min-width: 3em; }
#back { display: none; }
.newline { margin: 10px 0; text-align: center; color: var(--new); font-size: 12px; font-weight: bold;
  border-top: 2px solid var(--new); line-height: 0; }
.newline span { background: var(--bg); padding: 0 8px; }
.tmsg { padding: 20px; color: var(--sub); }

#posts { padding: 10px 14px 60vh; }
#posts, #gallery, .popup .post { zoom: var(--zoom, 1); }

/* posts */
.post { margin: 2px 0 10px; padding: 2px 0 6px; border-bottom: 1px solid var(--line); }
.post.flash { animation: flash 1.6s ease-out; }
@keyframes flash { 0% { box-shadow: 0 0 0 3px var(--title); } 100% { box-shadow: 0 0 0 3px transparent; } }
.ph { font-size: 13px; display: flex; flex-wrap: wrap; gap: 0 8px; align-items: baseline; }
.ph .no { font-weight: bold; cursor: pointer; }
.ph .no:hover { text-decoration: underline; }
.ph .no.hasref { color: var(--link); }
.ph .nm { color: var(--no); font-weight: bold; }
.ph .ml { color: var(--sub); font-size: 12px; }
.ph .dt { color: var(--sub); }
.ph .uid { color: var(--sub); cursor: pointer; }
.ph .uid:hover { text-decoration: underline; }
.ph .uid.many { color: var(--title); }
.ph .wc { color: var(--sub); cursor: pointer; font-size: .92em; }
.ph .wc:hover { text-decoration: underline; }
.ph .wc b { font-weight: normal; color: var(--no); }
.ph .wc.many b { font-weight: bold; color: var(--title); }
.popup .phead a { margin-left: 8px; }
.sr-hit .who { color: var(--sub); font-size: 11px; margin-right: 6px; }
.ph .br { color: var(--link); cursor: pointer; font-size: 12px; border-bottom: 1px dotted; }
.bd { margin: 4px 0 0 1.5em; white-space: pre-wrap; word-break: break-word; overflow-wrap: anywhere; }
.anc { color: var(--link); cursor: pointer; text-decoration: underline; }
.anc.nf { color: var(--sub); cursor: default; text-decoration: none; }
.q { color: var(--quote); }
.imgs { display: flex; flex-wrap: wrap; gap: 6px; margin: 6px 0 0 1.5em; }
.imgs a { display: block; cursor: zoom-in; }
.imgs img { max-width: 220px; max-height: 220px; display: block; border: 1px solid var(--line); background: var(--reply); }
.imgs img.bad { width: 80px; height: 40px; }

/* popups */
.popup {
  position: fixed; z-index: 50; background: var(--pop); border: 1px solid var(--line);
  box-shadow: 0 3px 14px var(--shadow); border-radius: 4px; padding: 4px 8px;
  max-width: min(720px, 94vw); max-height: 70vh; overflow-y: auto;
}
.popup .post { margin: 2px 0; border-bottom: 1px dashed var(--line); }
.popup .post:last-child { border-bottom: 0; }
.popup .imgs img { max-width: 120px; max-height: 120px; }
.popup .phead { font-size: 12px; color: var(--sub); border-bottom: 1px solid var(--line); margin-bottom: 2px; }

/* gallery */
#gallery { display: grid; grid-template-columns: repeat(auto-fill, minmax(120px, 1fr)); gap: 8px; padding: 10px 14px; }
#gallery .gi { cursor: pointer; background: var(--reply); border-radius: 4px; overflow: hidden; text-align: center; font-size: 11px; }
#gallery .gi img { width: 100%; height: 120px; object-fit: cover; display: block; }
#gallery .gi span { display: block; padding: 2px; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }

/* lightbox */
#lightbox { position: fixed; inset: 0; z-index: 100; background: rgba(0,0,0,.88); display: flex; align-items: center; justify-content: center; }
#lightbox[hidden] { display: none; }
#lbBody { max-width: 92vw; max-height: 86vh; display: flex; align-items: center; justify-content: center; }
#lbBody img { max-width: 92vw; max-height: 86vh; object-fit: contain; }
.lb-bar { position: absolute; top: 0; left: 0; right: 0; display: flex; justify-content: space-between; align-items: center;
  padding: 6px 12px; color: #eee; font-size: 13px; background: rgba(0,0,0,.5); gap: 8px; }
.lb-bar a { color: #9cf; margin: 0 8px; }
.lb-bar button { background: #333; color: #eee; border-color: #555; }
.lb-nav { position: absolute; top: 50%; transform: translateY(-50%); font-size: 40px; background: rgba(0,0,0,.4) !important;
  color: #fff !important; border: 0 !important; padding: 10px 16px; }
#lbPrev { left: 8px; } #lbNext { right: 8px; }

/* 板の切り替え（矢印だけの select。開くと板の一覧） */
#boardSel { width: 1.9em; padding: 2px 0; margin-left: -4px; color: var(--title); font-weight: bold; text-align: center; }
#boardSel option { color: var(--fg); font-weight: normal; }
.bdg { font-size: 11px; border: 1px solid var(--line); border-radius: 3px; padding: 0 4px; margin-right: 4px; color: var(--sub); }

/* 自分の書き込み・返信 */
:root { --mine: #e9f3ff; --mineb: #3b82f6; --tome: #fff1e0; --tomeb: #ea7a10; }
@media (prefers-color-scheme: dark) { :root:not([data-theme="light"]) { --mine: #1c2a3d; --tome: #3a2a17; } }
:root[data-theme="dark"] { --mine: #1c2a3d; --tome: #3a2a17; }
.post.mine { background: var(--mine); box-shadow: inset 3px 0 0 var(--mineb); padding-left: 6px; }
.post.tome { background: var(--tome); box-shadow: inset 3px 0 0 var(--tomeb); padding-left: 6px; }
.anc.me { color: var(--tomeb); font-weight: bold; }
.ph .br.me { color: var(--tomeb); font-weight: bold; }
.ph .pops { margin-left: auto; display: inline-flex; gap: 8px; font-size: 12px; }
.ph .rp, .ph .metg { color: var(--sub); cursor: pointer; user-select: none; }
.ph .rp:hover, .ph .metg:hover { color: var(--link); text-decoration: underline; }
.ph .metg.on { color: var(--mineb); font-weight: bold; }
@media (hover: hover) {
  .post:not(:hover) .pops .rp, .post:not(:hover) .pops .metg:not(.on) { opacity: 0; }
}
#meBtn.on { background: var(--sel); font-weight: bold; }
#meCount:not(:empty) { margin-left: 4px; background: var(--tomeb); color: #fff; border-radius: 9px; padding: 0 6px; font-size: 12px; font-weight: bold; }
.me-r, .me-h, .me-d { padding: 6px 10px; border-bottom: 1px solid var(--line); cursor: pointer; }
.me-r:hover, .me-h:hover, .me-d:hover { background: var(--reply); }
.me-r.new { background: var(--tome); box-shadow: inset 3px 0 0 var(--tomeb); }
.me-head { font-size: 13px; }
.me-head .n { color: var(--no); font-weight: bold; }
.me-head small { color: var(--sub); }
.me-empty { padding: 12px; color: var(--sub); font-size: 13px; }
#sideHead button { padding: 0 8px; font-size: 12px; margin-left: 4px; }

/* 書き込み欄（下に固定。一覧の横＝スレの幅に合わせる） */
#compose {
  position: fixed; left: 361px; right: 0; bottom: 0; z-index: 30; background: var(--side);
  border-top: 2px solid var(--title); box-shadow: 0 -3px 12px var(--shadow); padding: 6px 12px 8px;
}
#compose[hidden] { display: none; }
.cp-head { display: flex; align-items: center; gap: 6px; font-size: 13px; }
.cp-head b { color: var(--title); white-space: nowrap; overflow: hidden; text-overflow: ellipsis; min-width: 0; }
.cp-head button { padding: 0 8px; }
.cp-head .grow, .cp-row .grow { flex: 1; }
#compose.min .cp-body { display: none; }
.cp-body { display: flex; flex-direction: column; gap: 5px; margin-top: 5px; }
.cp-row { display: flex; gap: 6px; align-items: center; flex-wrap: wrap; }
#compose input:not([type]), #compose textarea {
  padding: 4px 6px; border: 1px solid var(--line); border-radius: 4px; background: var(--bg); color: inherit; font: inherit;
}
#cpName { flex: 2 1 140px; min-width: 0; } #cpMail { flex: 1 1 90px; min-width: 0; }
#cpSubject[hidden] { display: none; }
#cpMsg { width: 100%; resize: vertical; min-height: 6.5em; max-height: 50vh; line-height: 1.5; }
#cpSend.primary { background: var(--title); border-color: var(--title); color: #fff; font-weight: bold; padding: 4px 16px; }
#cpSend:disabled { opacity: .6; }
#cpInfo, #cpCount { font-size: 12px; color: var(--sub); }
#cpBox { font-size: 13px; padding: 6px 8px; border-radius: 4px; border: 1px solid var(--line); }
#cpBox.err { border-color: #d33; background: rgba(220, 50, 50, .08); }
#cpBox.err b { color: #d33; }
#cpBox.warn { border-color: var(--tomeb); background: var(--tome); }
#cpBox .cp-msg { white-space: pre-wrap; margin: 3px 0; max-height: 8em; overflow-y: auto; font-size: 12px; }
body.composing #posts { padding-bottom: 75vh; }
#toast {
  position: fixed; left: 50%; bottom: 24px; transform: translateX(-50%); z-index: 120; background: var(--fg); color: var(--bg);
  padding: 8px 16px; border-radius: 6px; font-size: 13px; box-shadow: 0 3px 12px var(--shadow); max-width: 90vw;
}
#toast[hidden] { display: none; }
body.composing #toast { bottom: auto; top: 60px; }

@media (max-width: 800px) {
  #compose { left: 0; padding: 4px 8px 6px; }
  #cpMsg { min-height: 5em; max-height: 35vh; }
  body.reading #boardSel, body.reading #meBtn { display: none; }
  #meBtn { padding: 1px 5px; font-size: 12px; }
  .ph .pops { gap: 10px; }
  #side { width: 100%; border-right: 0; }
  #view { display: none; }
  body.reading #side { display: none; }
  body.reading #view { display: block; }
  #back { display: inline-block; }
  #posts { padding: 8px 8px 50vh; }
  .imgs img { max-width: 140px; max-height: 140px; }
  .bd, .imgs { margin-left: .3em; }
  #tfind { width: 120px; }

  /* 読んでいる間はバーを詰める（スレタイをタップで詳細を出す） */
  #tbar { padding: 4px 8px; gap: 4px 6px; flex-wrap: nowrap; }
  #tbar.expand { flex-wrap: wrap; }
  #tbar #back { order: -1; padding: 2px 7px; white-space: nowrap; }
  #tbar .ttl { flex: 1 1 0; min-width: 4em; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; font-size: 13px; }
  #tbar.expand .ttl { flex: 1 1 calc(100% - 5em); white-space: normal; }
  #tbar .info, #tbar .grow, #tbar #tfindCount:empty { display: none; }
  #tbar.expand .info { display: inline; }
  #tbar button { padding: 2px 6px; }
  #tbar #tfind { width: auto; flex: 0 1 90px; min-width: 60px; }
  #tbar:not(.expand) #galBtn, #tbar:not(.expand) #toTop, #tbar:not(.expand) #toEnd,
  #tbar:not(.expand) #tfPrev, #tbar:not(.expand) #tfNext { display: none; }
  #tbar:not(.expand) #tfindCount:not(:empty) ~ #tfPrev, #tbar:not(.expand) #tfindCount:not(:empty) ~ #tfNext { display: inline-block; }
  .zoom button { padding: 2px 5px; }
  .zoom .zoomLbl { min-width: 2.8em; font-size: 11px; }
  #top { padding: 4px 8px; gap: 4px 6px; }
  #home { font-size: 14px; }
  #searchForm button { padding: 2px 6px; }
  #sort, #bmBtn, #refreshBtn { padding: 1px 5px; font-size: 12px; }
  #sideHead { padding: 4px 8px; }
  #cats button { padding: 8px 2px; }
  body.reading #sort, body.reading #bmBtn, body.reading #refreshBtn, body.reading #status { display: none; }
  body.reading #top { padding: 4px 8px; gap: 6px; flex-wrap: nowrap; }
  body.reading #home { font-size: 13px; }
  body.reading #searchForm { flex: 1 1 0; min-width: 0; }
  body.reading #q { min-width: 0; }
  body.reading #searchForm button { padding: 2px 6px; }
}

/* Local Ruri semantic search */
#searchMode{flex:0 0 auto;min-width:4em} #q{min-width:0} #searchForm{min-width:160px} .sr-hit{overflow-wrap:anywhere} #semanticHelp[hidden]{display:none} @media(max-width:800px){#searchMode{padding:2px 4px;min-width:3.8em}}
