:root {
  --bg: var(--tg-theme-bg-color, #14181f);
  --text: var(--tg-theme-text-color, #eae2c8);
  --hint: var(--tg-theme-hint-color, #8a8f98);
  --button: var(--tg-theme-button-color, #c9a227);
  --button-text: var(--tg-theme-button-text-color, #14181f);
  --secondary-bg: var(--tg-theme-secondary-bg-color, #1e2530);
}

* { box-sizing: border-box; }

body {
  margin: 0;
  background: var(--bg);
  color: var(--text);
  font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, sans-serif;
}

#focusApp, #eventApp, #decisionApp, #ideaApp {
  display: flex;
  flex-direction: column;
  gap: 10px;
  padding: 10px;
  min-height: 100vh;
}

.app-panel {
  display: flex;
  flex-direction: column;
  gap: 10px;
  padding: 10px;
  padding-top: 48px;
  min-height: 100vh;
}

.mode-screen {
  display: flex;
  flex-direction: column;
  align-items: stretch;
  gap: 16px;
  padding: 48px 16px 24px;
  min-height: 100vh;
}

.mode-screen h1 {
  margin: 0;
  font-size: 1.35rem;
  text-align: center;
}

.mode-cards {
  display: flex;
  flex-direction: column;
  gap: 12px;
}

.mode-card {
  display: flex;
  flex-direction: column;
  align-items: flex-start;
  gap: 6px;
  padding: 16px;
  border-radius: 14px;
  border: 1px solid #3a4150;
  background: var(--secondary-bg);
  color: var(--text);
  text-align: left;
  cursor: pointer;
}

.mode-card-icon {
  font-size: 1.75rem;
}

.mode-card-title {
  font-size: 1.1rem;
  font-weight: 600;
}

.mode-card-desc {
  font-size: 0.85rem;
  color: var(--hint);
  line-height: 1.35;
}

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

.panel-title {
  font-weight: 600;
  font-size: 1rem;
}

.back-btn {
  min-width: 36px;
  padding: 6px 10px;
}

.event-kind-row {
  display: flex;
  gap: 8px;
  flex-wrap: wrap;
}

.kind-btn {
  flex: 1;
  min-width: 140px;
}

.kind-btn.active {
  border-color: var(--button);
  background: color-mix(in srgb, var(--button) 18%, var(--secondary-bg));
}

.form-grid {
  display: grid;
  grid-template-columns: 1fr 1fr;
  gap: 10px;
}

.field {
  display: flex;
  flex-direction: column;
  gap: 4px;
  font-size: 0.82rem;
  color: var(--hint);
}

.field-full {
  grid-column: 1 / -1;
}

.field-narrow {
  max-width: 120px;
}

.field input,
.field textarea {
  width: 100%;
  padding: 8px 10px;
  border-radius: 8px;
  border: 1px solid #3a4150;
  background: var(--secondary-bg);
  color: var(--text);
  font-size: 0.95rem;
}

.event-flags {
  display: flex;
  flex-wrap: wrap;
  gap: 10px 16px;
  align-items: center;
}

.check-field {
  display: flex;
  align-items: center;
  gap: 6px;
  font-size: 0.88rem;
}

.event-picture-block,
.event-options-block {
  border: 1px solid #2a3140;
  border-radius: 12px;
  padding: 12px;
  background: #0d1017;
}

.event-picture-head,
.event-options-head {
  display: flex;
  justify-content: space-between;
  align-items: center;
  gap: 8px;
  margin-bottom: 8px;
}

.event-preview {
  max-width: 100%;
  max-height: 160px;
  border-radius: 8px;
  margin-top: 8px;
  border: 1px solid #3a4150;
}

.event-option-row {
  display: flex;
  align-items: center;
  gap: 8px;
  margin-bottom: 6px;
}

.event-option-key {
  font-weight: 600;
  min-width: 1.2em;
}

.event-option-name {
  flex: 1;
  padding: 8px 10px;
  border-radius: 8px;
  border: 1px solid #3a4150;
  background: var(--secondary-bg);
  color: var(--text);
}

.event-option-remove {
  padding: 4px 10px;
}

.btn {
  background: var(--secondary-bg);
  color: var(--text);
  border: 1px solid #3a4150;
  border-radius: 10px;
  padding: 10px 14px;
  font-size: 14px;
  cursor: pointer;
}

.btn.primary {
  background: var(--button);
  color: var(--button-text);
  border-color: var(--button);
  font-weight: 600;
}

.btn.danger {
  background: #5a2323;
  border-color: #7a3030;
  color: #f0d0d0;
}

.btn.finish {
  background: var(--button);
  color: var(--button-text);
  font-weight: 700;
  padding: 14px;
  border-radius: 12px;
  font-size: 15px;
}

.file-btn {
  display: inline-flex;
  align-items: center;
  justify-content: center;
}

input[type="text"] {
  background: var(--secondary-bg);
  color: var(--text);
  border: 1px solid #3a4150;
  border-radius: 10px;
  padding: 10px 12px;
  font-size: 14px;
  flex: 1;
  min-width: 0;
}

#topbar, #toolbar, #pendingBar .pending-actions, #renameBox {
  display: flex;
  gap: 8px;
  flex-wrap: wrap;
  align-items: center;
}

#topbar input#countryTagInput {
  width: 4.5em;
  text-transform: uppercase;
  font-weight: 600;
  letter-spacing: 0.05em;
}

#topbar input#treeIdInput[readonly] {
  opacity: 0.85;
  cursor: default;
}

#canvasWrap {
  overflow: auto;
  border: 1px solid #2a3140;
  border-radius: 12px;
  background: #0d1017;
  min-height: 200px;
  max-height: 42vh;
  padding: 12px;
  cursor: grab;
}

#canvasWrap.panning {
  cursor: grabbing;
  user-select: none;
}

#canvasWrap .focus-box {
  cursor: pointer;
}

#canvasWrap .focus-box.pending {
  cursor: grab;
}

#canvasWrap .focus-box.pending.dragging {
  cursor: grabbing;
}

#gridScaler {
  position: relative;
  display: inline-block;
}

#grid {
  position: relative;
  transform-origin: 0 0;
}

#linesLayer {
  position: absolute;
  top: 0;
  left: 0;
  pointer-events: none;
  overflow: visible;
}

.hint-text {
  font-size: 12px;
  color: var(--hint);
  margin: 0;
  line-height: 1.4;
}

.cell {
  position: absolute;
  width: 90px;
  height: 90px;
  border: 1px dashed #232b38;
  box-sizing: border-box;
}

.focus-box {
  position: absolute;
  width: 78px;
  min-height: 78px;
  background: #2e3a4a;
  border: 2px solid #c9a227;
  border-radius: 10px;
  color: #f0e6c8;
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  text-align: center;
  font-size: 10px;
  padding: 3px;
  gap: 2px;
  user-select: none;
  touch-action: none;
  cursor: default;
  overflow: hidden;
}

.focus-box .focus-icon {
  width: 44px;
  height: 44px;
  object-fit: contain;
  pointer-events: none;
  flex-shrink: 0;
}

.focus-box .focus-name {
  line-height: 1.1;
  max-height: 24px;
  overflow: hidden;
  word-break: break-word;
  pointer-events: none;
}

.focus-box:not(.pending) {
  cursor: pointer;
}

.focus-box.pending {
  background: #6e2828;
  border-color: #eb5a3c;
  cursor: grab;
  z-index: 5;
  box-shadow: 0 0 0 3px rgba(235, 90, 60, 0.25);
}

.focus-box.pending.dragging {
  cursor: grabbing;
  opacity: 0.85;
}

.focus-box.linking-source {
  box-shadow: 0 0 0 3px rgba(90, 180, 235, 0.6);
}

.focus-box.linking-target-hover {
  box-shadow: 0 0 0 3px rgba(90, 235, 120, 0.8);
}

.focus-box.mutex-source {
  box-shadow: 0 0 0 3px rgba(224, 69, 90, 0.85);
}

.focus-box.selected {
  box-shadow: 0 0 0 3px rgba(120, 180, 255, 0.85);
}

.focus-box.pending.selected {
  box-shadow: 0 0 0 3px rgba(120, 180, 255, 0.85);
  border-color: #c9a227;
}

.hint {
  position: absolute;
  top: 50%;
  left: 50%;
  transform: translate(-50%, -50%);
  color: var(--hint);
  font-size: 13px;
  white-space: nowrap;
}

#pendingBar {
  background: var(--secondary-bg);
  border-radius: 12px;
  padding: 10px 12px;
  display: flex;
  flex-direction: column;
  gap: 8px;
}

#pendingHeader {
  display: flex;
  align-items: center;
  gap: 10px;
}

.icon-preview-inline {
  width: 44px;
  height: 44px;
  border: 2px solid #c9a227;
  border-radius: 8px;
  background: #0d1017;
  object-fit: contain;
  flex-shrink: 0;
}

.icon-preview-inline.placeholder {
  display: flex;
  align-items: center;
  justify-content: center;
  font-size: 22px;
}

.icon-panel {
  border: 1px solid #3a4150;
  border-radius: 10px;
  padding: 8px;
  display: flex;
  flex-direction: column;
  gap: 6px;
  background: #0d1017;
}

.reward-panel {
  border: 1px solid #3a4150;
  border-radius: 10px;
  padding: 8px;
  display: flex;
  flex-direction: column;
  gap: 6px;
  background: #0d1017;
}

.reward-panel-title,
.icon-panel-title {
  font-size: 13px;
  font-weight: 600;
  color: var(--text, #eae2c8);
}

.reward-toolbar {
  display: flex;
  gap: 6px;
}

.reward-toolbar input {
  flex: 1;
  background: var(--secondary-bg, #1e2530);
  color: var(--text, #eae2c8);
  border: 1px solid #3a4150;
  border-radius: 8px;
  padding: 8px 10px;
  font-size: 13px;
}

.reward-suggest {
  max-height: 180px;
  overflow: auto;
  display: flex;
  flex-direction: column;
  gap: 4px;
  border: 1px solid #3a4150;
  border-radius: 8px;
  padding: 4px;
  background: #14181f;
}

.reward-suggest.hidden {
  display: none !important;
}

.reward-suggest-item {
  text-align: left;
  background: transparent;
  color: var(--text, #eae2c8);
  border: 1px solid transparent;
  border-radius: 6px;
  padding: 6px 8px;
  font-size: 12px;
  cursor: pointer;
}

.reward-suggest-item:hover,
.reward-suggest-item.active {
  border-color: var(--button, #c9a227);
  background: #2a3344;
}

.reward-suggest-item strong {
  display: block;
}

.reward-suggest-item span {
  color: var(--hint, #8a8f98);
  font-size: 11px;
}

.reward-desc {
  min-height: 1.4em;
  margin: 0;
}

.reward-editor {
  width: 100%;
  font-family: ui-monospace, Consolas, monospace;
  font-size: 12px;
  line-height: 1.4;
  background: var(--secondary-bg, #1e2530);
  color: var(--text, #eae2c8);
  border: 1px solid #3a4150;
  border-radius: 8px;
  padding: 8px;
  resize: vertical;
  white-space: pre;
}

.reward-actions {
  display: flex;
  gap: 6px;
}

.icon-panel-title {
  font-size: 13px;
  font-weight: 600;
  color: var(--hint);
}

.icon-panel-head {
  display: flex;
  gap: 6px;
  flex-wrap: wrap;
  align-items: center;
}

.icon-panel-head input[type="search"],
.icon-panel-head input[type="text"] {
  flex: 1;
  min-width: 120px;
  background: var(--secondary-bg);
  color: var(--text);
  border: 1px solid #3a4150;
  border-radius: 8px;
  padding: 8px 10px;
  font-size: 13px;
}

#iconGrid {
  display: grid;
  grid-template-columns: repeat(auto-fill, minmax(68px, 1fr));
  gap: 6px;
  overflow-y: auto;
  max-height: 180px;
  padding: 2px;
}

.icon-choice {
  border: 2px solid #3a4150;
  border-radius: 8px;
  background: var(--secondary-bg);
  padding: 3px;
  cursor: pointer;
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 2px;
}

.icon-choice:hover,
.icon-choice.selected {
  border-color: var(--button);
}

.icon-choice img {
  width: 52px;
  height: 52px;
  object-fit: contain;
}

.icon-choice span {
  font-size: 8px;
  color: var(--hint);
  text-align: center;
  line-height: 1.1;
  max-height: 16px;
  overflow: hidden;
  word-break: break-all;
}

#pendingLabel {
  font-weight: 600;
  font-size: 14px;
  line-height: 1.3;
}

select {
  width: 100%;
  background: var(--secondary-bg);
  color: var(--text);
  border: 1px solid #3a4150;
  border-radius: 10px;
  padding: 10px;
  font-size: 14px;
}

.hidden { display: none !important; }

.lang-toggle {
  position: fixed;
  top: 10px;
  right: 10px;
  z-index: 200;
  padding: 6px 10px;
  font-size: 13px;
}

/* --- Decisions (HOI4-like screen) --- */
.decision-list-block {
  border: 1px solid #3a4150;
  border-radius: 10px;
  padding: 8px;
  background: #0d1017;
  display: flex;
  flex-direction: column;
  gap: 8px;
}

.decision-list-head {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 8px;
  font-weight: 600;
  font-size: 13px;
}

.decision-list {
  display: flex;
  flex-direction: column;
  gap: 4px;
  max-height: 160px;
  overflow: auto;
}

.decision-list-item {
  text-align: left;
  background: transparent;
  color: var(--text);
  border: 1px solid transparent;
  border-radius: 8px;
  padding: 8px 10px;
  cursor: pointer;
  display: flex;
  flex-direction: column;
  gap: 2px;
}

.decision-list-item:hover {
  background: #2a3344;
}

.decision-list-item.active {
  border-color: var(--button);
  background: color-mix(in srgb, var(--button) 14%, var(--secondary-bg));
}

.decision-list-item strong {
  font-size: 13px;
}

.decision-list-item span {
  font-size: 11px;
  color: var(--hint);
  font-family: ui-monospace, Consolas, monospace;
}

.decision-editor {
  display: flex;
  flex-direction: column;
  gap: 12px;
}

.decision-meta-aside {
  border: 1px dashed #4a5568;
  border-radius: 10px;
  padding: 10px;
  background: var(--secondary-bg);
  display: flex;
  flex-direction: column;
  gap: 8px;
}

.decision-meta-row {
  display: flex;
  flex-wrap: wrap;
  gap: 10px;
  align-items: flex-end;
}

.decision-once {
  padding-bottom: 8px;
}

.hoi4-decision-screen {
  position: relative;
  border: 2px solid #6b5a2e;
  border-radius: 4px;
  padding: 14px 16px 12px;
  background:
    linear-gradient(180deg, rgba(42, 36, 24, 0.95) 0%, rgba(28, 24, 18, 0.98) 100%),
    repeating-linear-gradient(
      0deg,
      transparent,
      transparent 2px,
      rgba(0, 0, 0, 0.08) 2px,
      rgba(0, 0, 0, 0.08) 3px
    );
  box-shadow:
    inset 0 0 0 1px rgba(201, 162, 39, 0.25),
    0 8px 24px rgba(0, 0, 0, 0.45);
  display: flex;
  flex-direction: column;
  gap: 10px;
}

.hoi4-decision-header {
  display: flex;
  align-items: center;
  gap: 10px;
}

.hoi4-decision-icon {
  width: 36px;
  height: 36px;
  border-radius: 4px;
  border: 1px solid #8a7340;
  background: #1a1610;
  display: flex;
  align-items: center;
  justify-content: center;
  font-size: 1.25rem;
  flex-shrink: 0;
}

.hoi4-decision-title {
  flex: 1;
  background: transparent;
  border: none;
  border-bottom: 1px solid transparent;
  color: #f0e6c8;
  font-size: 1.05rem;
  font-weight: 700;
  font-family: "Segoe UI", Georgia, serif;
  padding: 4px 2px;
  outline: none;
}

.hoi4-decision-title:focus {
  border-bottom-color: #c9a227;
}

.hoi4-decision-desc {
  width: 100%;
  background: rgba(0, 0, 0, 0.25);
  border: 1px solid #4a4030;
  border-radius: 2px;
  color: #d8d0b8;
  font-size: 13px;
  line-height: 1.45;
  padding: 10px;
  resize: vertical;
  min-height: 88px;
  font-family: inherit;
}

.hoi4-decision-footer {
  display: flex;
  gap: 8px;
  align-items: center;
  flex-wrap: wrap;
}

.hoi4-pp-badge,
.hoi4-days-badge {
  font-size: 12px;
  font-weight: 600;
  padding: 4px 8px;
  border-radius: 2px;
  border: 1px solid #6b5a2e;
  background: #1a1610;
  color: #e8d9a0;
}

.hoi4-days-badge.hidden {
  display: none !important;
}

/* --- National spirits (ideas) --- */
.hoi4-idea-screen {
  position: relative;
  border: 2px solid #4a6b8a;
  border-radius: 4px;
  padding: 14px 16px 12px;
  background:
    linear-gradient(180deg, rgba(28, 36, 48, 0.96) 0%, rgba(18, 24, 34, 0.98) 100%),
    repeating-linear-gradient(
      0deg,
      transparent,
      transparent 2px,
      rgba(0, 0, 0, 0.08) 2px,
      rgba(0, 0, 0, 0.08) 3px
    );
  box-shadow:
    inset 0 0 0 1px rgba(120, 160, 200, 0.22),
    0 8px 24px rgba(0, 0, 0, 0.45);
  display: flex;
  flex-direction: column;
  gap: 10px;
}

.hoi4-idea-icon {
  width: 36px;
  height: 36px;
  border-radius: 4px;
  border: 1px solid #6a8aaa;
  background: #121820;
  display: flex;
  align-items: center;
  justify-content: center;
  font-size: 1.25rem;
  flex-shrink: 0;
}
