:root {
  color-scheme: dark;
  --ink: #f7f4ea;
  --muted: #b8c2bd;
  --panel: #171b1c;
  --panel-soft: #202729;
  --line: #3c4748;
  --court: #2f8a64;
  --court-deep: #257150;
  --clay: #ba6f3e;
  --blue: #2d8cff;
  --coral: #ff6b5f;
  --gold: #f5cf46;
}

* {
  box-sizing: border-box;
}

html,
body {
  height: 100%;
  overflow: hidden;
}

body {
  margin: 0;
  background: #101313;
  color: var(--ink);
  font-family: Inter, ui-sans-serif, system-ui, -apple-system, BlinkMacSystemFont, "Segoe UI", sans-serif;
}

button {
  border: 0;
  color: inherit;
  font: inherit;
}

.app-shell {
  display: grid;
  grid-template-columns: 112px minmax(0, 1fr);
  min-height: 100dvh;
  height: 100dvh;
}

.tool-panel {
  display: flex;
  flex-direction: column;
  gap: 18px;
  align-items: stretch;
  min-width: 0;
  padding: 16px 12px max(16px, env(safe-area-inset-bottom));
  background: var(--panel);
  border-right: 1px solid var(--line);
}

.brand {
  display: grid;
  justify-items: center;
  gap: 7px;
  margin-top: auto;
  text-align: center;
  line-height: 1;
}

.brand-logo {
  display: grid;
  width: 74px;
  max-width: 100%;
  height: auto;
}

.brand-link {
  display: inline-grid;
  place-items: center;
  color: inherit;
  text-decoration: none;
}

.tool-group,
.action-grid {
  display: grid;
  grid-template-columns: repeat(2, minmax(0, 1fr));
  gap: 9px;
}

.tool-group {
  margin-top: 98px;
}

.tool-button,
.action-grid button {
  display: grid;
  place-items: center;
  width: 40px;
  height: 40px;
  margin: 0 auto;
  background: var(--panel-soft);
  border: 1px solid transparent;
  border-radius: 7px;
  cursor: pointer;
}

.tool-button svg,
.action-grid svg {
  width: 21px;
  height: 21px;
  fill: none;
  stroke: currentColor;
  stroke-width: 2;
  stroke-linecap: round;
  stroke-linejoin: round;
}

.tool-button:nth-child(2) svg,
.tool-button:nth-child(3) svg {
  fill: currentColor;
  stroke-width: 1.5;
}

.tool-button:hover,
.action-grid button:hover,
.segmented button:hover {
  border-color: #738080;
}

.tool-button.is-active,
.segmented button.is-active {
  background: var(--ink);
  color: #111;
}

.control-stack {
  display: grid;
  gap: 10px;
}

.segmented {
  display: grid;
  gap: 6px;
}

.surface-segmented {
  grid-template-columns: 1fr;
}

.segmented button {
  min-height: 34px;
  padding: 0 6px;
  background: var(--panel-soft);
  border: 1px solid transparent;
  border-radius: 7px;
  cursor: pointer;
  font-size: 12px;
}

.board-area {
  display: grid;
  grid-template-rows: auto auto minmax(0, 1fr);
  min-width: 0;
  min-height: 0;
  background:
    linear-gradient(90deg, rgba(186, 111, 62, 0.24), transparent 28%, transparent 72%, rgba(45, 140, 255, 0.12)),
    #111615;
}

.top-bar {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 16px;
  min-height: 56px;
  padding: 10px 18px;
  border-bottom: 1px solid var(--line);
  background: rgba(16, 19, 19, 0.86);
}

.top-bar div {
  display: flex;
  flex-wrap: wrap;
  gap: 6px 12px;
  align-items: center;
}

.top-logo {
  width: 86px;
  height: auto;
  display: block;
}

.top-bar h1 {
  margin: 0;
  font-size: 15px;
  line-height: 1.1;
  font-weight: 700;
}

.top-bar span,
.top-bar output {
  color: var(--muted);
  font-size: 13px;
}

.frame-bar {
  display: flex;
  align-items: center;
  gap: 8px;
  min-height: 42px;
  padding: 6px 18px;
  border-bottom: 1px solid var(--line);
  background: rgba(23, 27, 28, 0.9);
}

.frame-spacer {
  flex: 1 1 auto;
  min-width: 12px;
}

.frame-bar button {
  display: grid;
  place-items: center;
  width: 32px;
  height: 30px;
  background: var(--panel-soft);
  border: 1px solid transparent;
  border-radius: 7px;
  cursor: pointer;
}

.frame-bar button:hover {
  border-color: #738080;
}

.frame-bar button:disabled {
  color: #687373;
  cursor: not-allowed;
  opacity: 0.5;
}

.frame-bar button:disabled:hover {
  border-color: transparent;
}

.frame-bar svg {
  width: 18px;
  height: 18px;
  fill: none;
  stroke: currentColor;
  stroke-width: 2;
  stroke-linecap: round;
  stroke-linejoin: round;
}

#frameLabel {
  min-width: 88px;
  color: var(--muted);
  font-size: 13px;
  text-align: center;
}

.export-menu,
.app-menu {
  position: fixed;
  z-index: 15;
  display: grid;
  gap: 6px;
  min-width: 190px;
  padding: 8px;
  background: var(--panel);
  border: 1px solid var(--line);
  border-radius: 8px;
  box-shadow: 0 16px 46px rgba(0, 0, 0, 0.36);
}

.export-menu[hidden],
.app-menu[hidden] {
  display: none;
}

.app-menu.is-open {
  display: grid !important;
  z-index: 35;
}

.export-menu button,
.app-menu button,
.app-menu a {
  min-height: 34px;
  padding: 0 10px;
  color: var(--ink);
  background: var(--panel-soft);
  border: 1px solid transparent;
  border-radius: 7px;
  cursor: pointer;
  font-size: 12px;
  text-align: left;
  text-decoration: none;
  display: flex;
  align-items: center;
}

.export-menu button:hover,
.app-menu button:hover,
.app-menu a:hover {
  border-color: #738080;
}

.app-dialog,
.content-dialog {
  position: fixed;
  inset: 0;
  z-index: 20;
  display: grid;
  place-items: center;
  padding: 18px;
  background: rgba(0, 0, 0, 0.58);
}

.app-dialog[hidden],
.content-dialog[hidden] {
  display: none;
}

.app-dialog__panel,
.content-dialog__panel {
  width: min(420px, 100%);
  padding: 18px;
  background: var(--panel);
  border: 1px solid var(--line);
  border-radius: 8px;
  box-shadow: 0 22px 70px rgba(0, 0, 0, 0.38);
}

.content-dialog__panel {
  width: min(760px, 100%);
  max-height: min(720px, calc(100dvh - 36px));
  display: grid;
  grid-template-rows: auto auto minmax(0, 1fr);
}

.content-dialog__header {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 12px;
  margin-bottom: 12px;
}

.content-dialog__header h2 {
  margin: 0;
  font-size: 17px;
}

.content-dialog__header button {
  display: grid;
  place-items: center;
  flex: 0 0 auto;
  width: 34px;
  height: 34px;
  color: var(--ink);
  background: var(--panel-soft);
  border: 1px solid transparent;
  border-radius: 7px;
  cursor: pointer;
}

.content-dialog__header svg {
  width: 18px;
  height: 18px;
  fill: none;
  stroke: currentColor;
  stroke-width: 2;
  stroke-linecap: round;
  stroke-linejoin: round;
}

.content-dialog__tabs {
  display: flex;
  gap: 8px;
  overflow-x: auto;
  padding-bottom: 10px;
  scrollbar-width: thin;
}

.content-dialog__tabs button {
  flex: 0 0 auto;
  min-height: 34px;
  padding: 0 12px;
  color: var(--ink);
  background: var(--panel-soft);
  border: 1px solid transparent;
  border-radius: 7px;
  cursor: pointer;
  font-size: 12px;
}

.content-dialog__tabs button.is-active {
  color: #111;
  background: var(--ink);
}

.content-dialog__body {
  overflow: auto;
  padding: 14px;
  background: rgba(255, 255, 255, 0.04);
  border: 1px solid var(--line);
  border-radius: 8px;
}

.content-dialog__body h3 {
  margin: 0 0 8px;
  font-size: 16px;
}

.content-dialog__body h4 {
  margin: 18px 0 6px;
  font-size: 13px;
}

.content-dialog__body p,
.content-dialog__body li {
  color: var(--muted);
  font-size: 14px;
  line-height: 1.48;
}

.content-dialog__body p {
  margin: 0 0 12px;
}

.content-dialog__body ul {
  margin: 0 0 12px;
  padding-left: 20px;
}

.content-dialog__body a {
  color: var(--ink);
}

.content-dialog__header button:hover,
.content-dialog__tabs button:hover {
  border-color: #738080;
}

.app-dialog h2 {
  margin: 0 0 8px;
  font-size: 17px;
}

.app-dialog p {
  margin: 0 0 14px;
  color: var(--muted);
  font-size: 14px;
  line-height: 1.45;
}

.app-dialog input {
  display: block;
  width: 100%;
  height: 38px;
  margin-bottom: 14px;
  padding: 0 10px;
  color: var(--ink);
  background: var(--panel-soft);
  border: 1px solid var(--line);
  border-radius: 7px;
  font: inherit;
}

.app-dialog input[hidden] {
  display: none;
}

.app-dialog__actions {
  display: flex;
  justify-content: flex-end;
  gap: 8px;
}

.app-dialog__actions button {
  min-width: 88px;
  min-height: 34px;
  padding: 0 12px;
  background: var(--panel-soft);
  border: 1px solid transparent;
  border-radius: 7px;
  cursor: pointer;
  color: var(--ink);
  font-size: 12px;
}

.app-dialog__actions button:last-child {
  background: var(--ink);
  color: #111;
}

.app-dialog__actions button:hover,
.app-dialog input:focus {
  border-color: #738080;
  outline: 0;
}

#courtCanvas {
  display: block;
  width: 100%;
  height: 100%;
  min-height: 0;
  cursor: crosshair;
  touch-action: none;
}

.canvas-wrap {
  position: relative;
  min-width: 0;
  min-height: 0;
}

.status-badge {
  position: absolute;
  z-index: 2;
  top: 12px;
  left: 12px;
  max-width: min(280px, calc(100% - 24px));
  padding: 6px 10px;
  color: var(--muted);
  background: rgba(16, 19, 19, 0.72);
  border: 1px solid rgba(255, 255, 255, 0.14);
  border-radius: 7px;
  font-size: 12px;
  line-height: 1.2;
  pointer-events: none;
  backdrop-filter: blur(6px);
}

@media (min-width: 1280px) {
  .app-shell {
    grid-template-columns: 124px minmax(0, 1fr);
  }

  .tool-button,
  .action-grid button {
    width: 44px;
    height: 44px;
  }
}

@media (max-width: 1024px) {
  .app-shell {
    grid-template-columns: 1fr;
    grid-template-rows: auto minmax(0, 1fr);
    height: 100dvh;
  }

  .tool-panel {
    display: grid;
    grid-template-columns: auto minmax(330px, 1fr) auto auto;
    align-items: center;
    gap: 10px;
    overflow-x: auto;
    padding: 9px 10px;
    border-right: 0;
    border-bottom: 1px solid var(--line);
    scrollbar-width: thin;
  }

  .brand {
    grid-auto-flow: column;
    align-items: center;
    margin-top: 0;
    text-align: left;
  }

  .brand-logo {
    width: 76px;
  }

  .tool-group {
    display: flex;
    margin-top: 0;
    min-width: max-content;
  }

  .tool-button,
  .action-grid button {
    width: 40px;
    height: 40px;
  }

  .control-stack {
    grid-template-columns: repeat(3, max-content);
  }

  .segmented {
    grid-template-columns: repeat(2, 70px);
  }

  .surface-segmented {
    grid-template-columns: repeat(3, 58px);
  }

  .action-grid {
    display: flex;
    min-width: max-content;
  }

  .top-bar {
    min-height: 48px;
    padding: 8px 12px;
  }

  .frame-bar {
    min-height: 40px;
    padding: 5px 12px;
  }
}

@media (max-width: 640px) {
  .app-shell {
    grid-template-rows: minmax(0, 1fr) auto;
  }

  .board-area {
    order: 1;
  }

  .tool-panel {
    order: 2;
    grid-template-columns: auto minmax(252px, 1fr) auto;
    grid-template-areas:
      "brand tools actions"
      "modes modes modes";
    padding: 8px max(8px, env(safe-area-inset-right)) max(8px, env(safe-area-inset-bottom)) max(8px, env(safe-area-inset-left));
    border-top: 1px solid var(--line);
    border-bottom: 0;
  }

  .brand {
    grid-area: brand;
  }

  .brand-logo {
    width: 36px;
  }

  .tool-group {
    grid-area: tools;
    gap: 7px;
    overflow-x: auto;
    padding-bottom: 2px;
    scrollbar-width: none;
  }

  .tool-group::-webkit-scrollbar {
    display: none;
  }

  .action-grid {
    grid-area: actions;
    gap: 7px;
  }

  .control-stack {
    grid-area: modes;
    display: grid;
    grid-template-columns: 1fr 1fr;
    gap: 8px;
    min-width: 100%;
  }

  .segmented {
    grid-template-columns: 1fr 1fr;
  }

  .surface-segmented {
    grid-column: 1 / -1;
    grid-template-columns: repeat(3, 1fr);
  }

  .segmented button {
    min-height: 32px;
    font-size: 12px;
  }

  .tool-button,
  .action-grid button {
    flex: 0 0 auto;
    width: 38px;
    height: 38px;
  }

  .tool-button svg,
  .action-grid svg {
    width: 20px;
    height: 20px;
  }

  .top-bar {
    align-items: flex-start;
    gap: 4px;
    min-height: 54px;
    padding: 8px 10px;
  }

  .top-bar,
  .top-bar div {
    flex-direction: column;
  }

  .top-bar h1 {
    font-size: 14px;
  }

  .top-logo {
    width: 78px;
  }

  .top-bar span,
  .top-bar output {
    font-size: 12px;
  }

  .status-badge {
    top: 8px;
    left: 8px;
    max-width: calc(100% - 16px);
    padding: 5px 8px;
  }

  .frame-bar {
    gap: 6px;
    overflow-x: auto;
    padding: 5px 10px;
  }

  .frame-bar button {
    flex: 0 0 auto;
    width: 34px;
    height: 32px;
  }

  #frameLabel {
    flex: 0 0 auto;
    min-width: 80px;
    font-size: 12px;
  }

  .frame-spacer {
    flex: 0 0 8px;
  }

  .content-dialog {
    padding: 10px;
  }

  .content-dialog__panel {
    max-height: calc(100dvh - 20px);
    padding: 14px;
  }

  .content-dialog__body {
    padding: 12px;
  }
}

@media (max-width: 390px) {
  .tool-panel {
    grid-template-columns: minmax(0, 1fr) auto;
    grid-template-areas:
      "tools actions"
      "modes modes";
  }

  .brand {
    display: none;
  }

  .control-stack {
    grid-template-columns: 1fr;
  }

  .top-bar {
    min-height: 50px;
  }
}
