:root {
  --bg: #f4f6f8;
  --surface: #ffffff;
  --surface-2: #eef1f4;
  --line: #d9dfe5;
  --ink: #1b2530;
  --muted: #5c6878;
  --accent: #0e6699;
  --accent-ink: #ffffff;
  --accent-soft: #e2f0f8;
  --danger: #b83232;
  --danger-soft: #fbeaea;
  --ok: #1d7a4d;
  --ok-soft: #e4f4ec;
  --warn: #92610a;
  --warn-soft: #fbf1dc;
  --shadow: 0 1px 2px rgba(20, 30, 40, .06), 0 4px 14px rgba(20, 30, 40, .06);
  --radius: 8px;
  --mono: Consolas, "Cascadia Mono", monospace;
  color-scheme: light;
}

@media (prefers-color-scheme: dark) {
  :root:not([data-theme="light"]) {
    --bg: #12171d;
    --surface: #1b222a;
    --surface-2: #252e38;
    --line: #333e4a;
    --ink: #e6ebf0;
    --muted: #9aa7b5;
    --accent: #4aa6db;
    --accent-ink: #06202f;
    --accent-soft: #17344a;
    --danger: #ef7d7d;
    --danger-soft: #3b1f22;
    --ok: #5fcf9a;
    --ok-soft: #173327;
    --warn: #e6b65c;
    --warn-soft: #3a2e15;
    --shadow: 0 1px 2px rgba(0, 0, 0, .3), 0 4px 14px rgba(0, 0, 0, .25);
    color-scheme: dark;
  }
}

* { box-sizing: border-box; }
[hidden] { display: none !important; }

html, body { height: 100%; }
body {
  margin: 0;
  background: var(--bg);
  color: var(--ink);
  font: 14px/1.6 "Yu Gothic UI", "Meiryo UI", Meiryo, system-ui, sans-serif;
}

h1, h2, h3 { margin: 0; line-height: 1.35; font-weight: 700; }
h1 { font-size: 20px; }
h2 { font-size: 16px; }
h3 { font-size: 14px; }
a { color: var(--accent); }
.muted { color: var(--muted); }
.small { font-size: 12.5px; }
.mono { font-family: var(--mono); }
.nowrap { white-space: nowrap; }
.break { overflow-wrap: anywhere; }

/* ---------- 共通部品 ---------- */
button, .btn {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  gap: 6px;
  min-height: 34px;
  padding: 5px 14px;
  border: 1px solid var(--line);
  border-radius: 6px;
  background: var(--surface);
  color: var(--ink);
  font: inherit;
  text-decoration: none;
  white-space: nowrap;
  cursor: pointer;
}
button:hover:not(:disabled), .btn:hover { background: var(--surface-2); }
button:disabled { opacity: .55; cursor: default; }
button.primary, .btn.primary { background: var(--accent); border-color: var(--accent); color: var(--accent-ink); font-weight: 600; }
button.primary:hover:not(:disabled), .btn.primary:hover { filter: brightness(1.08); background: var(--accent); }
button.danger { color: var(--danger); border-color: var(--danger); }
button.danger:hover:not(:disabled) { background: var(--danger-soft); }
button.link { border: 0; background: none; color: var(--accent); padding: 0; min-height: 0; }
button.link:hover:not(:disabled) { background: none; text-decoration: underline; }
:focus-visible { outline: 2px solid var(--accent); outline-offset: 2px; }

input, select, textarea {
  width: 100%;
  min-height: 34px;
  padding: 5px 9px;
  border: 1px solid var(--line);
  border-radius: 6px;
  background: var(--surface);
  color: var(--ink);
  font: inherit;
}
input[type="checkbox"] { width: auto; min-height: 0; }
label.field { display: block; margin-bottom: 12px; }
label.field > span { display: block; margin-bottom: 3px; font-size: 12.5px; color: var(--muted); }
label.check { display: inline-flex; align-items: center; gap: 8px; }

.card {
  background: var(--surface);
  border: 1px solid var(--line);
  border-radius: var(--radius);
  padding: 18px;
}
.card + .card { margin-top: 16px; }
.card > h2 { margin-bottom: 12px; }
.card.created { margin-bottom: 16px; }

.error { color: var(--danger); background: var(--danger-soft); padding: 8px 12px; border-radius: 6px; margin-bottom: 12px; }
.notice { color: var(--ok); background: var(--ok-soft); padding: 8px 12px; border-radius: 6px; margin-bottom: 12px; }

.badge { display: inline-block; padding: 1px 8px; border-radius: 999px; font-size: 12px; font-weight: 600; white-space: nowrap; background: var(--surface-2); color: var(--muted); }
.badge.ok { background: var(--ok-soft); color: var(--ok); }
.badge.warn { background: var(--warn-soft); color: var(--warn); }
.badge.bad { background: var(--danger-soft); color: var(--danger); }
.badge.info { background: var(--accent-soft); color: var(--accent); }

.table-wrap { overflow-x: auto; }
table { width: 100%; border-collapse: collapse; }
th { text-align: left; font-size: 12.5px; font-weight: 600; color: var(--muted); white-space: nowrap; }
th, td { padding: 8px 10px; border-bottom: 1px solid var(--line); vertical-align: top; }
tbody tr.clickable { cursor: pointer; }
tbody tr.clickable:hover { background: var(--surface-2); }
.empty { padding: 26px 10px; text-align: center; color: var(--muted); }
.row { display: flex; gap: 10px; align-items: center; flex-wrap: wrap; }
.row.end { justify-content: flex-end; }
.spacer { flex: 1; }

/* ---------- ログイン ---------- */
.login { min-height: 100vh; display: grid; grid-template-columns: minmax(0, 1fr); place-items: center; padding: 20px; }
.login .card { width: 100%; max-width: 380px; box-shadow: var(--shadow); }
.login h1 { margin-bottom: 4px; }
.login p { margin: 0 0 18px; }

.login .step-title { margin: 8px 0 4px; }
.login .qr { display: block; margin: 0 auto 12px; background: #fff; padding: 6px; border-radius: 6px; }
.login details { margin-bottom: 12px; }
.sso-box { margin-top: 16px; }
.sso-box .or { text-align: center; color: var(--muted); font-size: 12.5px; margin-bottom: 8px; }
.sso-box .btn { width: 100%; }
.recovery { list-style: none; margin: 0 0 14px; padding: 10px 12px; display: grid; grid-template-columns: 1fr 1fr; gap: 4px 16px; background: var(--surface-2); border-radius: 6px; }
.form-note { margin: 10px 0 0; }
.cert-warn { color: var(--danger); font-weight: 600; }

/* ---------- アプリ枠 ---------- */
.shell { min-height: 100%; display: grid; grid-template-columns: 208px 1fr; }
.side { background: var(--surface); border-right: 1px solid var(--line); padding: 16px 10px; display: flex; flex-direction: column; gap: 2px; }
.side .brand { font-weight: 700; font-size: 15px; padding: 2px 10px 0; }
.side .tenant { font-size: 12.5px; color: var(--muted); padding: 0 10px 14px; overflow-wrap: anywhere; }
.side nav { display: flex; flex-direction: column; gap: 2px; }
.side nav a { display: block; padding: 7px 10px; border-radius: 6px; color: var(--ink); text-decoration: none; }
.side nav a:hover { background: var(--surface-2); }
.side nav a.current { background: var(--accent-soft); color: var(--accent); font-weight: 600; }
.side .who { margin-top: auto; padding: 12px 10px 0; border-top: 1px solid var(--line); font-size: 12.5px; }
.main { padding: 22px 24px 40px; min-width: 0; max-width: 1180px; }
.main > h1 { margin-bottom: 16px; }

.connect-grid { display: grid; grid-template-columns: minmax(260px, 340px) minmax(0, 1fr); gap: 16px; align-items: start; }
.connect-grid > .card { margin-top: 0; }
.code-input { font-family: var(--mono); font-size: 17px; letter-spacing: .06em; }

.filters { display: grid; grid-template-columns: repeat(auto-fit, minmax(150px, 1fr)); gap: 10px; align-items: end; margin-bottom: 14px; }
.filters label.field { margin: 0; }

dialog.modal {
  width: min(760px, calc(100vw - 32px));
  max-height: calc(100vh - 32px);
  overflow-y: auto;
  padding: 20px;
  color: var(--ink);
  background: var(--surface);
  border: 1px solid var(--line);
  border-radius: var(--radius);
  box-shadow: var(--shadow);
}
dialog.modal::backdrop { background: rgba(10, 15, 20, .5); }
.modal-section { margin-top: 18px; }
.modal-section > h3 { margin: 0 0 10px; }
.modal-user { padding: 14px 0; border-top: 1px solid var(--line); }
.modal-user .filters { margin-bottom: 12px; }

.timeline { list-style: none; margin: 0; padding: 0; }
.timeline li { display: grid; grid-template-columns: 76px 128px minmax(0, 1fr); gap: 10px; padding: 7px 0; border-bottom: 1px solid var(--line); }
.timeline li:last-child { border-bottom: 0; }
.meta-grid { display: grid; grid-template-columns: repeat(auto-fit, minmax(190px, 1fr)); gap: 10px 18px; margin-bottom: 16px; }
.meta-grid dt { font-size: 12.5px; color: var(--muted); }
.meta-grid dd { margin: 0; overflow-wrap: anywhere; }

.copy-row { display: flex; gap: 8px; }
.copy-row input { font-family: var(--mono); font-size: 12.5px; }

@media (max-width: 800px) {
  .shell { grid-template-columns: 1fr; }
  .side { border-right: 0; border-bottom: 1px solid var(--line); }
  .side nav { flex-direction: row; flex-wrap: wrap; }
  .side .who { margin-top: 10px; }
  .main { padding: 16px; }
  .connect-grid { grid-template-columns: 1fr; }
  .timeline li { grid-template-columns: 70px minmax(0, 1fr); }
  .timeline li > :nth-child(3) { grid-column: 1 / -1; }
}

/* ---------- 配布ページ ---------- */
.download-page { min-height: 100vh; display: grid; grid-template-columns: minmax(0, 1fr); place-items: center; padding: 20px; }
.download-page .card { width: 100%; max-width: 520px; box-shadow: var(--shadow); }
.download-page ol { padding-left: 20px; margin: 14px 0; }
.download-page .btn { width: 100%; min-height: 44px; font-size: 15px; }

/* ---------- 操作画面 ---------- */
.session { height: 100%; display: grid; grid-template-rows: auto minmax(0, 1fr); background: #0e1216; color: #e6ebf0; }
.toolbar { display: flex; align-items: center; gap: 8px; flex-wrap: wrap; padding: 6px 10px; background: #1a2027; border-bottom: 1px solid #2d3640; }
.toolbar .title { font-weight: 600; margin-right: 6px; overflow-wrap: anywhere; }
.toolbar select, .toolbar button { width: auto; min-height: 30px; padding: 2px 10px; background: #262e37; border-color: #3a4551; color: #e6ebf0; font-size: 13px; }
.toolbar select { max-width: 170px; }
.toolbar button:hover:not(:disabled) { background: #323c47; }
.toolbar button.primary, .toolbar button.primary:hover:not(:disabled) { background: var(--accent); border-color: var(--accent); color: var(--accent-ink); font-weight: 600; }
.toolbar button.end { color: #ffb4b4; border-color: #8a4040; }
.toolbar button.end:hover:not(:disabled) { background: #4a2326; }
.toolbar .stat { font-family: var(--mono); font-size: 12px; color: #9aa7b5; white-space: nowrap; }

.stage-wrap { display: grid; grid-template-columns: minmax(0, 1fr) 320px; min-height: 0; }
.stage { position: relative; display: flex; overflow: hidden; min-width: 0; background: #0e1216; }
.stage.actual { overflow: auto; }
.stage.dragging { outline: 3px dashed #4aa6db; outline-offset: -8px; }
#screen { display: block; margin: auto; flex: none; background: #000; touch-action: none; cursor: default; }
#screen:focus { outline: 2px solid #4aa6db; outline-offset: -2px; }

.overlay { position: absolute; inset: 0; display: grid; place-items: center; padding: 20px; background: rgba(10, 14, 18, .82); text-align: center; }
.overlay .box { max-width: 420px; }
.overlay h2 { margin-bottom: 8px; font-size: 18px; }
.overlay p { margin: 0 0 14px; color: #b9c4cf; }
.overlay button { background: #262e37; border-color: #3a4551; color: #e6ebf0; }
.overlay button:hover:not(:disabled) { background: #323c47; }

.banner { position: absolute; top: 10px; left: 50%; transform: translateX(-50%); max-width: 90%; padding: 5px 14px; border-radius: 6px; background: #5a4413; color: #fbe6b5; font-size: 13px; pointer-events: none; }

.panel { display: grid; grid-template-rows: minmax(0, 1fr) minmax(112px, auto); min-height: 0; background: var(--surface); color: var(--ink); border-left: 1px solid var(--line); }
.chat { display: grid; grid-template-rows: auto minmax(0, 1fr) auto; min-height: 0; }
.panel h3 { padding: 9px 12px 7px; border-bottom: 1px solid var(--line); }
.messages { overflow-y: auto; padding: 10px 12px; display: flex; flex-direction: column; gap: 8px; }
.msg { max-width: 88%; align-self: flex-start; }
.msg .bubble { padding: 6px 10px; border-radius: 8px; background: var(--surface-2); white-space: pre-wrap; overflow-wrap: anywhere; }
.msg .meta { font-size: 11.5px; color: var(--muted); margin-bottom: 1px; }
.msg.mine { align-self: flex-end; }
.msg.mine .bubble { background: var(--accent-soft); }
.msg.mine .meta { text-align: right; }
.sysmsg { align-self: center; font-size: 12px; color: var(--muted); text-align: center; }
.chat-input { display: grid; grid-template-columns: minmax(0, 1fr) auto; gap: 6px; padding: 8px 10px; border-top: 1px solid var(--line); }
.chat-input textarea { resize: none; height: 54px; }

.files { border-top: 1px solid var(--line); max-height: min(42vh, 320px); overflow-y: auto; }
.files .actions { display: flex; gap: 6px; padding: 8px 10px; flex-wrap: wrap; }
.files .actions button { flex: 1 1 auto; font-size: 13px; }
.transfer { padding: 7px 12px; border-top: 1px solid var(--line); font-size: 13px; }
.transfer .name { overflow-wrap: anywhere; }
.transfer progress { width: 100%; height: 6px; display: block; margin: 4px 0; accent-color: var(--accent); }
.transfer .row { gap: 8px; }
.transfer button { min-height: 26px; padding: 0 9px; font-size: 12.5px; }

@media (max-width: 860px) {
  .stage-wrap { grid-template-columns: 1fr; grid-template-rows: minmax(0, 1fr) 40%; }
  .panel { border-left: 0; border-top: 1px solid var(--line); }
}
