@layer base, layout, components, responsive;

@layer base {
  :root {
    color-scheme: dark;
    --bg:#111; --panel:#1b1b1b; --raised:#252525; --hover:#303030; --field:#151515;
    --line:#414141; --edge:#717171; --muted:#bfbfbf; --paper:#f4f4f4;
    --accent:#fafafa; --accent-hover:#ddd; --ink:#111; --selected:#373737;
    --good:#efefef; --danger:#fff; --radius:9px; --worktop:#0b0b0b;
    --lobby-light:#262626; --shadow:#0009; --icon-filter:invert(1); --icon-primary-filter:none;
    --board-top:#292929; --board-bottom:#181818; --board-grid:#8c8c8c; --board-frame:#929292;
    --board-ink:#f7f7f7; --board-back:#202020; --board-meter:#aaa; --board-grid-mark:#fff;
    font: 16px/1.45 "Segoe UI", system-ui, sans-serif;
  }
  :root[data-theme=light] {
    color-scheme: light;
    --bg:#f2f2f2; --panel:#fff; --raised:#f0f0f0; --hover:#e3e3e3; --field:#fff;
    --line:#cbcbcb; --edge:#777; --muted:#505050; --paper:#181818;
    --accent:#1e1e1e; --accent-hover:#3d3d3d; --ink:#fff; --selected:#dcdcdc;
    --good:#2b6044; --danger:#891f27; --worktop:#dadada;
    --lobby-light:#fff; --shadow:#0002; --icon-filter:none; --icon-primary-filter:invert(1);
    --board-top:#f2f2f2; --board-bottom:#e2e2e2; --board-grid:#777; --board-frame:#777;
    --board-ink:#161616; --board-back:#f6f6f6; --board-meter:#555; --board-grid-mark:#222;
  }
  *, *::before, *::after { box-sizing: border-box; }
  body { margin: 0; min-width: 300px; color: var(--paper); background: var(--bg); }
  button, input, select, textarea { font: inherit; color: inherit; }
  button, select { cursor: pointer; }
  button, .button { min-height: 40px; padding: .5rem .8rem; line-height: 1.15; border: 1px solid var(--line); background: var(--raised); border-radius: var(--radius); color: var(--paper); text-decoration: none; display: inline-flex; align-items: center; justify-content: center; gap: .42rem; white-space: nowrap; }
  button:hover, .button:hover { background: var(--hover); border-color: var(--edge); }
  button:disabled { opacity: .42; cursor: default; }
  button:focus-visible, a:focus-visible, input:focus-visible, select:focus-visible, textarea:focus-visible, canvas:focus-visible { outline: 2px solid var(--accent); outline-offset: 3px; }
  .primary { color: var(--ink); background: var(--accent); border-color: var(--accent); font-weight: 700; }
  .primary:hover { background: var(--accent-hover); }
  .active { color: var(--accent); border-color: var(--edge); background: var(--selected); }
  .danger { color: var(--danger); }
  .good { color: var(--good); }
  input, select, textarea { display: block; width: 100%; padding: .55rem .65rem; min-height: 40px; border: 1px solid var(--line); border-radius: 7px; background: var(--field); }
  input[type=checkbox] { width: 1.15rem; min-height: 0; height: 1.15rem; accent-color: var(--accent); }
  input[type=color] { padding: .18rem; height: 41px; }
  input[type=file] { padding: .3rem; }
  select { appearance: none; padding-right: 1.85rem; background: var(--field) url("data:image/svg+xml,%3Csvg xmlns='http://www.w3.org/2000/svg' width='12' height='8'%3E%3Cpath d='m1 1 5 5 5-5' stroke='%23808080' fill='none'/%3E%3C/svg%3E") right .6rem center / 12px 8px no-repeat; }
  textarea { resize: vertical; min-height: 100px; }
  label { display: grid; gap: .28rem; color: var(--muted); font-size: .9rem; min-width: 0; }
  label.check { display: flex; align-items: center; gap: .55rem; }
  h1,h2,h3,p { margin: 0; }
  h1,h2 { font-family: Georgia, "Times New Roman", serif; font-weight: 500; }
  h2 { font-size: 1.62rem; }
  h3 { font-size: 1rem; }
  p + p { margin-top: .6rem; }
  a { color: var(--accent); }
  :where([hidden]) { display: none !important; }
  .muted { color: var(--muted); }
  .small { font-size: .86rem; }
  .flow { display: grid; gap: .8rem; }
  .row { display: flex; align-items: center; flex-wrap: wrap; gap: .45rem; }
  .fields { display: grid; gap: .75rem; grid-template-columns: repeat(2,minmax(0,1fr)); }
  .full { grid-column: 1 / -1; }
  .d20 { object-fit: contain; filter: var(--icon-filter); }
  .primary .d20 { filter: var(--icon-primary-filter); }
  .icon { width: 21px; height: 21px; flex: none; }
  .iconButton { width: 40px; padding-inline: 0; }
}
@layer layout {
  #app { min-height: 100dvh; }
  .brand { display: flex; align-items: center; gap: .65rem; font-size: .84rem; font-weight: 700; letter-spacing: .12em; text-transform: uppercase; white-space: nowrap; }
  .brand img { width: 38px; height: 38px; }
  .lobby { min-height: 100dvh; display: grid; grid-template-rows: auto 1fr auto; max-width: 1280px; margin: auto; padding-inline: clamp(1rem,6.5vw,6rem); }
  body:has(.lobby) { background: radial-gradient(ellipse at 4% 42%,var(--lobby-light) 0,transparent 58%),var(--bg); }
  .lobby-header { display: flex; align-items: center; justify-content: space-between; padding-block: 1.25rem; border-bottom: 1px solid var(--line); }
  .lobby-main { display: grid; grid-template-columns: minmax(0,1.15fr) minmax(330px,.85fr); align-items: center; gap: clamp(1.5rem,6vw,6rem); padding: 2.8rem 0; }
  .intro h1 { font-size: clamp(2.85rem,6vw,5.7rem); line-height: .99; letter-spacing: -.04em; margin: 1.4rem 0; }
  .intro p { max-width: 28rem; font-size: 1.08rem; color: var(--muted); }
  .intro .eyeline { display: flex; align-items: center; gap: .6rem; color: var(--accent); font-size: .85rem; letter-spacing: .1em; text-transform: uppercase; }
  .eyeline::before { content: ""; display: inline-block; width: 28px; border-top: 1px solid var(--accent); }
  .lobby-card { background: var(--panel); border: 1px solid var(--edge); border-radius: 16px; padding: clamp(1rem,3vw,1.75rem); box-shadow: 0 24px 58px var(--shadow); }
  .lobby-card form .primary { width: 100%; margin-top: .3rem; min-height: 46px; }
  .lobby-footer { border-top: 1px solid var(--line); padding: 1rem 0; color: var(--muted); font-size: .9rem; }
  .recent { margin-top: 1.25rem; padding-top: 1rem; border-top: 1px solid var(--line); }
  .recent-item > :first-child { flex: 1; }
  .recent .button { justify-content: space-between; white-space: normal; text-align: left; }
  .room { height: 100dvh; display: grid; grid-template-rows: auto auto minmax(0,1fr) auto; overflow: hidden; }
  .room-header { display: flex; gap: 1rem; align-items: center; justify-content: space-between; padding: .65rem 1rem; background: var(--bg); border-bottom: 1px solid var(--line); }
  .room-header .left, .room-header .right { display: flex; gap: .6rem; align-items: center; min-width: 0; }
  .room-name { font-size: 1.35rem; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
  .room-header .brand { border-right: 1px solid var(--line); padding-right: .6rem; }
  .brand-home { text-decoration: none; color: var(--paper); }
  .room-meta { display: inline-flex; gap: .4rem; align-items: center; white-space: nowrap; color: var(--muted); font-size: .83rem; }
  .status { white-space: nowrap; color: var(--muted); font-size: .85rem; }
  .status::before { content: ""; display: inline-block; width: 7px; height: 7px; border-radius: 50%; margin-right: 6px; background: var(--danger); }
  .status[data-online=true]::before { background: var(--good); }
  .status[data-online=false]::before { background: transparent; border: 1px solid var(--danger); }
  #initiative { min-height: 57px; display: flex; align-items: center; gap: .65rem; padding: .45rem .8rem; background: var(--panel); border-bottom: 1px solid var(--line); }
  .order-title { min-width: 110px; flex: none; display: grid; font-size: .84rem; color: var(--muted); }
  #order-list { min-width: 0; flex: 1; display: flex; gap: .5rem; overflow: auto; scrollbar-width: thin; }
  #order-list button { min-height: 35px; font-size: .9rem; padding: .35rem .58rem; }
  .initiative-next { flex: none; max-width: 16ch; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
  .turn-token { border-color: var(--edge); color: var(--accent); background: var(--selected); }
  .picked { box-shadow: inset 0 -2px var(--accent); }
  .workspace { min-height: 0; display: grid; grid-template-columns: 66px minmax(0,1fr) 308px; position: relative; }
  .workspace:has(aside[hidden]) { grid-template-columns: 66px minmax(0,1fr); }
  .tools { display: flex; flex-direction: column; gap: .45rem; align-items: center; padding-top: .7rem; background: var(--panel); border-right: 1px solid var(--line); overflow-y: auto; }
  .tools button { min-height: 42px; width: 43px; padding: 0; }
  .tools hr { width: 31px; margin: .05rem 0; border: 0; border-top: 1px solid var(--line); }
  #board-shell { position: relative; display: grid; grid-template-rows: auto minmax(0,1fr); min-height: 0; min-width: 0; overflow: hidden; background: var(--worktop); }
  #board { display: block; width: 100%; height: 100%; touch-action: none; cursor: grab; }
  #board[data-tool=move], #board[data-tool=measure], #board[data-tool=mark], #board[data-tool=ping], #board[data-tool=area] { cursor: crosshair; }
  .board-meta { display: flex; flex-wrap: wrap; align-items: center; gap: .35rem; padding: .5rem .65rem .2rem; }
  .map-chip, .map-hint { padding: .41rem .62rem; border: 1px solid var(--edge); border-radius: 7px; background: var(--panel); color: var(--muted); }
  .map-hint { margin-top: 0; }
  .previewing :is(.tools, .dock, #initiative, aside) { visibility: hidden; }
  #selection-tools { gap: .35rem; }
  #selection-tools button { min-height: 34px; }
  #board[data-tool=group], #board[data-tool=trace] { cursor: crosshair; }
  .camera { position: absolute; right: .8rem; bottom: .8rem; display: flex; gap: .3rem; border: 1px solid var(--line); border-radius: 8px; padding: .25rem; background: var(--panel); align-items: center; }
  .camera button { min-height: 35px; width: 35px; padding: 0; }
  .camera output { min-width: 3.5rem; text-align: center; font-size: .85rem; color: var(--muted); }
  aside { min-width: 0; overflow: auto; background: var(--panel); border-left: 1px solid var(--line); padding: .8rem; }
  aside h3 { margin-bottom: .4rem; }
  .side-header { display: flex; justify-content: space-between; align-items: center; gap: .4rem; margin-bottom: .65rem; }
  .side-header button { min-height: 36px; padding: .35rem .5rem; }
  #inspector > .empty { border: 1px dashed var(--line); border-radius: var(--radius); padding: 1rem; }
  .dock { background: var(--panel); padding: .55rem .8rem; border-top: 1px solid var(--line); justify-content: space-between; align-items: center; display: flex; gap: .6rem; }
  .dice-form { display: flex; gap: .35rem; align-items: center; min-width: 0; }
  .dice-form input,.dice-form select { width: auto; min-width: 0; }
  .dice-form input.formula { width: 7rem; min-width: 4.5rem; }
  .dice-form input.count { width: 3.55rem; }
  .dice-form input.mod { width: 3.85rem; }
  .dock-actions { display: flex; gap: .4rem; }
}
@layer components {
  .tabs { display: flex; gap: .5rem; margin-bottom: .75rem; overflow: auto; }
  .tabs > button { flex: 1; }
  .divider { border-top: 1px solid var(--line); padding-top: .7rem; }
  .info { color: var(--muted); font-size: .92rem; }
  .token-head { display: flex; gap: .6rem; align-items: center; }
  .avatar { display: grid; place-items: center; flex: none; width: 42px; height: 42px; border-radius: 100%; border: 1px solid var(--edge); background: var(--raised); color: var(--accent); font-weight: 700; }
  .token-head h2 { overflow-wrap: anywhere; line-height: 1.08; }
  .token-list { display: grid; gap: .28rem; }
  .token-list button { min-height: 37px; justify-content: space-between; white-space: normal; text-align: left; }
  .token-list .name, .log p, .block p { overflow-wrap: anywhere; }
  .stats { display: grid; grid-template-columns: repeat(3,1fr); gap: .28rem; }
  .stats button, .stats .stat { display: grid; padding: .4rem .2rem; white-space: normal; text-align: center; font-size: .84rem; }
  .stat { border: 1px solid var(--line); border-radius: 7px; }
  .meter { height: 7px; border-radius: 5px; background: var(--line); overflow: hidden; }
  .meter span { display: block; height: 100%; background: var(--good); }
  .meter[data-zero=true] span { background: var(--danger); }
  .inline-form { display: flex; gap: .3rem; min-width: 0; }
  .inline-form input { min-width: 3.6rem; }
  .side-section { border-top: 1px solid var(--line); padding-top: .65rem; margin-top: .6rem; }
  .condition { font-size: .82rem; color: var(--accent); padding: .2rem .34rem; border: 1px solid var(--edge); border-radius: 6px; }
  .condition button { min-height: 24px; padding: .12rem .34rem; }
  dialog { border: 1px solid var(--edge); color: var(--paper); background: var(--panel); padding: 0; border-radius: 14px; width: min(560px,calc(100% - 1.5rem)); max-height: 90dvh; box-shadow: 0 18px 75px #0009; }
  dialog.wide { width: min(760px,calc(100% - 1.5rem)); }
  dialog::backdrop { background: #0009; }
  .dialog-head { position: sticky; z-index: 1; top: 0; display: flex; justify-content: space-between; align-items: center; gap: .5rem; background: var(--panel); border-bottom: 1px solid var(--line); padding: .8rem 1rem; }
  .dialog-main { padding: 1rem; }
  .dialog-actions { display: flex; flex-wrap: wrap; gap: .45rem; justify-content: flex-end; }
  .log { border-left: 2px solid var(--edge); padding: .38rem .5rem; background: var(--raised); }
  .log time { color: var(--muted); font-size: .81rem; }
  .log strong { color: var(--accent); }
  .die-value { display: inline-flex; margin: .16rem; padding: .1rem .36rem; border: 1px solid var(--line); border-radius: 5px; }
  .die-value.discarded { color: var(--muted); text-decoration: line-through; }
  .die-value.twenty { color: var(--good); }
  .die-value.one { color: var(--danger); }
  #toast { position: fixed; z-index: 5; right: .9rem; bottom: 5rem; max-width: min(27rem,calc(100vw - 1.8rem)); padding: .65rem .9rem; border-radius: 8px; background: var(--raised); border: 1px solid var(--edge); box-shadow: 0 10px 35px #0008; }
  #toast:empty { display: none; }
  #toast[data-error=true] { border-color: var(--accent); border-width: 2px; }
  .search-result { width: 100%; justify-content: space-between; text-align: left; white-space: normal; }
  .source { color: var(--muted); font-size: .82rem; }
  .look-link { display: flex; gap: .4rem; }
  .look-link input { min-width: 0; }
  .block { display: grid; gap: .65rem; }
  .action-item + .action-item { margin-top: .66rem; }
  .action-item p { white-space: pre-line; }
  .scroll-list { max-height: 45dvh; overflow: auto; }
}
@layer responsive {
  @media (max-width: 1120px) {
    .dice-form .die-input { display:none; }
    .room-meta .edition { display: none; }
  }
  @media (max-width: 900px) {
    .workspace, .workspace:has(aside[hidden]) { grid-template-columns: 54px minmax(0,1fr); }
    aside { z-index: 2; position: absolute; inset: 0 0 0 auto; width: min(320px,79vw); box-shadow: -8px 0 28px #0008; }
    .room-header .right { gap: .35rem; }
  }
  @media (max-width: 700px) {
    .lobby-main { grid-template-columns: 1fr; padding: 1.4rem 0 2rem; }
    .intro h1 { margin-block: .7rem; }
    .room-header { padding: .35rem .4rem; gap: .3rem; }
    .room-header .left, .room-header .right { gap: .27rem; }
    .room-header .brand { padding-right: .25rem; }
    .room-header .brand img { width: 31px; height: 31px; }
    .room-name { font-size: 1.13rem; }
    .room-header .invite-text, .room-meta, .status .connection-text, .initiative-next { display: none; }
    button,.button { min-height: 44px; }
    .dock { padding: .36rem; gap: .35rem; }
    .dock-actions { gap: .25rem; }
    .dock .journal-text { display: none; }
    #initiative { padding: .35rem; gap: .33rem; min-height: 46px; }
    #initiative > button { min-width: 40px; padding: .33rem; }
    #initiative .start-text { display: none; }
    .order-title { min-width: 4.4rem; font-size: .77rem; }
    .dice-form .advanced { display: none; }
    .dock .end-text { display: none; }
    .camera { right: .35rem; bottom: .35rem; }
    .camera output { min-width: 2.6rem; }
    .board-meta { padding: .3rem .35rem .1rem; }
  }
  @media (max-width: 440px) {
    .workspace, .workspace:has(aside[hidden]) { grid-template-columns: 46px minmax(0,1fr); }
    .tools button { width: 40px; }
    #dice-mini .dice-label { display: none; }
    .lobby-card { padding: .95rem; }
  }
  @media (max-height: 500px) and (orientation: landscape) {
    .room-header { padding-block: .18rem; }
    #initiative { min-height: 34px; padding-block: .18rem; }
    .dock { padding-block: .18rem; }
    dialog { max-height: 94dvh; }
  }
}
