:root {
  --bg: #f7efe2;
  --panel: #fffbf5;
  --ink: #1f3346;
  --muted: #667686;
  --accent: #b44d23;
  --accent-2: #0b7f95;
  --line: rgba(31, 51, 70, 0.14);
  --danger: #c33d2f;
  --shadow: 0 10px 28px rgba(31, 51, 70, 0.12);
  --font-ui: "Avenir Next", "Gill Sans", "Segoe UI", sans-serif;
}

* {
  box-sizing: border-box;
}

html,
body {
  margin: 0;
  min-height: 100%;
}

body {
  background:
    radial-gradient(circle at 12% 18%, rgba(180, 77, 35, 0.2), transparent 26rem),
    radial-gradient(circle at 84% 8%, rgba(11, 127, 149, 0.14), transparent 24rem),
    linear-gradient(180deg, #fff9ee 0%, #f5ecdc 100%);
  color: var(--ink);
  font-family: var(--font-ui);
}

body.scroll-locked {
  overflow: hidden;
  overscroll-behavior: none;
}

button,
input,
select,
textarea {
  font: inherit;
}

.hidden {
  display: none !important;
}

.app-page {
  max-width: 1680px;
  margin: 0 auto;
  padding: 18px;
}

.topbar {
  display: flex;
  align-items: baseline;
  justify-content: space-between;
  gap: 14px;
  margin-bottom: 14px;
}

.topbar h1 {
  margin: 0;
  font-size: clamp(1.6rem, 2.2vw, 2.25rem);
}

.topbar p {
  margin: 0;
  color: var(--muted);
  font-size: 0.93rem;
}

.workspace {
  display: grid;
  grid-template-columns: minmax(300px, 380px) minmax(0, 1fr) minmax(260px, 330px);
  grid-template-areas: "left center right";
  align-items: start;
  gap: 16px;
}

.left-column,
.center-column,
.right-column {
  display: grid;
  gap: 16px;
  align-content: start;
  min-width: 0;
}

.left-column {
  grid-area: left;
}

.center-column {
  grid-area: center;
  position: relative;
  z-index: 1;
}

.right-column {
  grid-area: right;
  position: relative;
  z-index: 2;
}

.card {
  background: var(--panel);
  border: 1px solid var(--line);
  border-radius: 16px;
  box-shadow: var(--shadow);
  padding: 14px;
  min-width: 0;
}

.card h2 {
  margin: 0 0 10px;
  font-size: 1.05rem;
}

.login-card {
  max-width: 460px;
}

.login-form {
  display: grid;
  gap: 10px;
}

.form-grid {
  display: grid;
  grid-template-columns: repeat(2, minmax(0, 1fr));
  gap: 10px;
}

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

#machine-select.machine-connected {
  border-color: rgba(42, 139, 88, 0.7);
  background: linear-gradient(180deg, #fbfffc, #f1fbf5);
}

#machine-select.machine-disconnected {
  border-color: rgba(195, 61, 47, 0.55);
  background: linear-gradient(180deg, #fffafa, #fff3f2);
}

.machine-health-list {
  display: grid;
  gap: 6px;
  margin-top: 10px;
}

.machine-health-item {
  width: 100%;
  border: 1px solid var(--line);
  border-radius: 10px;
  padding: 7px 9px;
  display: flex;
  justify-content: space-between;
  align-items: center;
  background: #fff;
  color: var(--ink);
  cursor: pointer;
}

.machine-health-item.active {
  border-color: rgba(11, 127, 149, 0.45);
  box-shadow: inset 0 0 0 1px rgba(11, 127, 149, 0.2);
}

.machine-health-pill {
  display: inline-flex;
  align-items: center;
  gap: 5px;
  font-size: 0.78rem;
  font-weight: 700;
  border-radius: 999px;
  padding: 3px 8px;
}

.machine-health-pill::before {
  content: "";
  width: 8px;
  height: 8px;
  border-radius: 50%;
  display: inline-block;
}

.machine-health-pill.connected {
  background: rgba(42, 139, 88, 0.12);
  color: #22724b;
}

.machine-health-pill.connected::before {
  background: #2a8b58;
}

.machine-health-pill.disconnected {
  background: rgba(195, 61, 47, 0.12);
  color: #a3352a;
}

.machine-health-pill.disconnected::before {
  background: #c33d2f;
}

label {
  display: grid;
  gap: 5px;
  font-size: 0.9rem;
}

label span {
  color: var(--muted);
}

input,
select,
textarea {
  width: 100%;
  border: 1px solid var(--line);
  border-radius: 10px;
  background: #fff;
  color: var(--ink);
  padding: 9px 10px;
}

button {
  border: 0;
  border-radius: 999px;
  padding: 9px 14px;
  cursor: pointer;
  background: #2b455e;
  color: #fff;
  transition: transform 120ms ease, opacity 120ms ease;
}

button:hover {
  transform: translateY(-1px);
}

button.primary {
  background: linear-gradient(120deg, var(--accent), #8b310f);
}

button.ghost {
  background: rgba(31, 51, 70, 0.09);
  color: var(--ink);
}

.button-row {
  display: flex;
  gap: 8px;
  margin-top: 10px;
}

.button-row.wrap {
  flex-wrap: wrap;
}

.status {
  margin: 8px 0 0;
  color: var(--muted);
  font-size: 0.9rem;
  min-height: 1.2rem;
}

.hint {
  margin: 8px 0 0;
  color: var(--muted);
  font-size: 0.84rem;
}

.scene-head {
  display: flex;
  justify-content: space-between;
  align-items: baseline;
  gap: 8px;
  margin-bottom: 8px;
}

.scene-head p {
  margin: 0;
  color: var(--muted);
  font-size: 0.9rem;
}

.editor-stage {
  height: 560px;
  border: 1px dashed rgba(31, 51, 70, 0.24);
  border-radius: 12px;
  background:
    linear-gradient(180deg, rgba(255, 255, 255, 0.94), rgba(247, 240, 227, 0.92)),
    repeating-linear-gradient(
      90deg,
      rgba(31, 51, 70, 0.05) 0,
      rgba(31, 51, 70, 0.05) 1px,
      transparent 1px,
      transparent 24px
    );
  overflow: hidden;
  touch-action: none;
  overscroll-behavior: contain;
  -webkit-user-select: none;
  user-select: none;
}

.element-list {
  list-style: none;
  padding: 0;
  margin: 12px 0 0;
  display: grid;
  gap: 8px;
  max-height: 200px;
  overflow: auto;
}

.element-item {
  display: grid;
  grid-template-columns: 1fr auto;
  align-items: center;
  gap: 8px;
  padding: 8px 9px;
  border: 1px solid var(--line);
  border-radius: 10px;
  background: rgba(255, 255, 255, 0.88);
}

.element-item.active {
  border-color: rgba(11, 127, 149, 0.55);
  background: rgba(11, 127, 149, 0.11);
}

.element-main {
  cursor: pointer;
}

.element-title {
  margin: 0;
  font-weight: 700;
  font-size: 0.92rem;
}

.element-subtitle {
  margin: 2px 0 0;
  font-size: 0.82rem;
  color: var(--muted);
}

.element-delete {
  background: rgba(195, 61, 47, 0.15);
  color: var(--danger);
}

.inspector.empty {
  color: var(--muted);
  font-size: 0.9rem;
}

.inspector {
  max-height: 360px;
  overflow: auto;
  scrollbar-gutter: stable;
}

.inspector-grid {
  display: grid;
  gap: 10px;
}

.checkbox {
  display: flex;
  align-items: center;
  gap: 8px;
}

.checkbox input {
  width: auto;
}

.history-grid,
.library-grid {
  display: grid;
  gap: 10px;
}

.history-card {
  padding-top: 10px;
}

.history-panel summary {
  cursor: pointer;
  font-weight: 700;
  list-style: none;
  margin-bottom: 8px;
}

.history-panel summary::-webkit-details-marker {
  display: none;
}

.history-panel summary::before {
  content: "▸";
  margin-right: 7px;
  color: var(--muted);
}

.history-panel[open] summary::before {
  content: "▾";
}

.history-panel .history-grid {
  max-height: 340px;
  overflow: auto;
  padding-right: 2px;
}

.history-item,
.library-item {
  border: 1px solid var(--line);
  border-radius: 10px;
  background: #fff;
  padding: 8px;
  display: grid;
  gap: 6px;
}

.history-item {
  grid-template-columns: 88px minmax(0, 1fr);
  grid-template-areas:
    "thumb title"
    "thumb meta"
    "thumb actions";
  column-gap: 10px;
  row-gap: 4px;
  align-items: start;
}

.history-item.no-thumb {
  grid-template-columns: 1fr;
  grid-template-areas:
    "title"
    "meta"
    "actions";
}

.history-item img {
  grid-area: thumb;
  width: 88px;
  min-width: 88px;
  height: 66px;
  object-fit: cover;
  border-radius: 8px;
  border: 1px solid rgba(31, 51, 70, 0.12);
  background: #fff;
}

.library-item img {
  width: 100%;
  border-radius: 8px;
  border: 1px solid rgba(31, 51, 70, 0.12);
  background: #fff;
}

.history-meta,
.library-meta {
  font-size: 0.82rem;
  color: var(--muted);
}

.library-title,
.history-title {
  margin: 0;
  font-size: 0.9rem;
  font-weight: 700;
}

.history-item .history-title {
  grid-area: title;
}

.history-item .history-meta {
  grid-area: meta;
  margin: 0;
}

.history-item .button-row {
  grid-area: actions;
  margin-top: 2px;
  flex-wrap: wrap;
}

.konvajs-content canvas {
  image-rendering: auto;
}

.editor-stage .konvajs-content,
.editor-stage .konvajs-content canvas,
.editor-stage .delete-handle {
  touch-action: none;
  -webkit-user-select: none;
  user-select: none;
}

.editor-stage .konvajs-content canvas {
  -webkit-user-drag: none;
}

.delete-handle {
  position: absolute;
  width: 24px;
  height: 24px;
  border-radius: 50%;
  border: 0;
  background: var(--danger);
  color: #fff;
  font-weight: 700;
  line-height: 1;
  padding: 0;
  display: grid;
  place-items: center;
  z-index: 8;
}


@media (max-width: 1320px) {
  .workspace {
    grid-template-columns: 1fr;
    grid-template-areas:
      "center"
      "left"
      "right";
  }

  .editor-stage {
    height: 460px;
  }
}
