/* === Reset & Base === */
*, *::before, *::after {
  box-sizing: border-box;
  margin: 0;
  padding: 0;
}

:root {
  --primary: #4a90d9;
  --primary-hover: #357abd;
  --bg: #f0f2f5;
  --panel-bg: #ffffff;
  --border: #dde1e6;
  --text: #1a1a2e;
  --text-secondary: #555;
  --accent: #e60012;
  --radius: 8px;
  --shadow: 0 2px 8px rgba(0,0,0,0.08);
}

body {
  font-family: 'Noto Sans JP', 'Hiragino Kaku Gothic ProN', 'Yu Gothic', sans-serif;
  background: var(--bg);
  color: var(--text);
  line-height: 1.6;
  min-height: 100vh;
}

/* === Header === */
header {
  background: linear-gradient(135deg, #1a1a2e 0%, #16213e 100%);
  color: #fff;
  padding: 16px 24px;
  text-align: center;
}

header h1 {
  font-size: 1.4rem;
  font-weight: 700;
  letter-spacing: 0.05em;
}

/* === App Container === */
.app-container {
  display: grid;
  grid-template-columns: 380px 1fr;
  gap: 0;
  min-height: calc(100vh - 60px);
  transition: grid-template-columns 0.25s ease;
}

.app-container.panel-collapsed {
  grid-template-columns: 0px 1fr;
}

/* === Controls Panel === */
.controls-panel {
  background: var(--panel-bg);
  border-right: 1px solid var(--border);
  padding: 16px;
  overflow-y: auto;
  max-height: calc(100vh - 60px);
  position: relative;
  transition: padding 0.25s ease, opacity 0.2s ease;
}

.controls-panel.collapsed {
  padding: 0;
  overflow: hidden;
  opacity: 0;
  pointer-events: none;
}

.panel-toggle {
  display: none;
}

.panel-header {
  display: flex;
  align-items: center;
  justify-content: space-between;
  margin-bottom: 12px;
  padding-bottom: 8px;
  border-bottom: 1px solid var(--border);
  position: sticky;
  top: -16px;
  background: var(--panel-bg);
  z-index: 5;
  padding-top: 4px;
}

.panel-header-title {
  font-size: 0.9rem;
  font-weight: 700;
  color: var(--text);
}

.panel-collapse-btn {
  padding: 4px 12px;
  background: #fff;
  color: var(--text-secondary);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  font-family: inherit;
  font-size: 0.75rem;
  cursor: pointer;
  transition: all 0.15s;
  flex-shrink: 0;
}

.panel-collapse-btn:hover {
  background: #f0f2f5;
  border-color: #999;
}

.panel-open-btn {
  position: fixed;
  top: 70px;
  left: 8px;
  z-index: 100;
  padding: 8px 14px;
  background: var(--panel-bg);
  color: var(--text);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  font-family: inherit;
  font-size: 0.85rem;
  font-weight: 500;
  cursor: pointer;
  box-shadow: 0 2px 8px rgba(0,0,0,0.15);
  transition: all 0.15s;
}

.panel-open-btn:hover {
  background: #f0f2f5;
  box-shadow: 0 2px 12px rgba(0,0,0,0.2);
}

.panel-open-btn.hidden {
  display: none;
}

.panel-section {
  margin-bottom: 20px;
  padding-bottom: 16px;
  border-bottom: 1px solid var(--border);
}

.panel-section:last-child {
  border-bottom: none;
}

.section-title {
  font-size: 0.95rem;
  font-weight: 700;
  color: var(--text);
  margin-bottom: 12px;
  padding-bottom: 4px;
  border-bottom: 2px solid var(--primary);
  display: inline-block;
}

/* === Form Controls === */
label {
  display: block;
  font-size: 0.85rem;
  font-weight: 500;
  color: var(--text-secondary);
  margin-bottom: 4px;
  margin-top: 10px;
}

label:first-child {
  margin-top: 0;
}

input[type="text"],
textarea,
select {
  width: 100%;
  padding: 8px 10px;
  border: 1px solid var(--border);
  border-radius: var(--radius);
  font-family: inherit;
  font-size: 0.9rem;
  background: #fafbfc;
  transition: border-color 0.2s;
}

input[type="text"]:focus,
textarea:focus,
select:focus {
  outline: none;
  border-color: var(--primary);
  box-shadow: 0 0 0 3px rgba(74, 144, 217, 0.15);
}

textarea {
  resize: vertical;
  font-size: 0.85rem;
  line-height: 1.5;
}

/* === Buttons === */
.btn {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  padding: 10px 20px;
  border: none;
  border-radius: var(--radius);
  font-family: inherit;
  font-size: 0.9rem;
  font-weight: 500;
  cursor: pointer;
  transition: all 0.2s;
}

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

.btn-row .btn {
  flex: 1;
}

.btn-primary {
  background: var(--primary);
  color: #fff;
}

.btn-primary:hover {
  background: var(--primary-hover);
}

.btn-secondary {
  background: #fff;
  color: var(--text-secondary);
  border: 1px solid var(--border);
}

.btn-secondary:hover {
  background: #f0f2f5;
  border-color: #bbb;
}

.btn-danger {
  width: 100%;
  margin-top: 6px;
  background: #fff;
  color: #d32f2f;
  border: 1px solid #e0e0e0;
  font-size: 0.8rem;
  padding: 6px 12px;
}

.btn-danger:hover {
  background: #ffebee;
  border-color: #d32f2f;
}

.btn-reset {
  width: 100%;
  margin-top: 10px;
  background: #fff;
  color: var(--text-secondary);
  border: 1px solid var(--border);
  font-size: 0.8rem;
  padding: 6px 12px;
}

.btn-reset:hover {
  background: #f0f2f5;
  border-color: #999;
}

.btn-export {
  background: linear-gradient(135deg, #e60012 0%, #c9000f 100%);
  color: #fff;
  padding: 12px 32px;
  font-size: 1rem;
  font-weight: 700;
}

.btn-export:hover {
  transform: translateY(-1px);
  box-shadow: 0 4px 12px rgba(230, 0, 18, 0.3);
}

/* === Control Row === */
.control-row {
  display: flex;
  align-items: center;
  gap: 8px;
  margin-bottom: 8px;
  flex-wrap: wrap;
}

.control-row label {
  margin: 0;
  min-width: 120px;
  flex-shrink: 0;
}

.control-row input[type="range"] {
  flex: 1;
  min-width: 80px;
}

.control-row input[type="color"] {
  width: 36px;
  height: 30px;
  padding: 2px;
  border: 1px solid var(--border);
  border-radius: 4px;
  cursor: pointer;
}

.control-row select {
  width: auto;
  flex: 1;
}

.control-row span {
  font-size: 0.85rem;
  color: var(--text-secondary);
  min-width: 30px;
  text-align: right;
}

.btn-step {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  width: 26px;
  height: 26px;
  border: 1px solid var(--border);
  border-radius: 4px;
  background: #fff;
  font-size: 1rem;
  cursor: pointer;
  color: var(--text);
  flex-shrink: 0;
  transition: all 0.15s;
  padding: 0;
  line-height: 1;
}

.btn-step:hover {
  background: #f0f2f5;
  border-color: #999;
}

/* === Segments List === */
#segments-list {
  max-height: 300px;
  overflow-y: auto;
}

.placeholder-text {
  color: #aaa;
  font-size: 0.85rem;
  text-align: center;
  padding: 20px 0;
}

.segment-row {
  display: flex;
  align-items: center;
  gap: 8px;
  padding: 6px 4px;
  border-bottom: 1px solid #f0f0f0;
  font-size: 0.85rem;
}

.segment-row:last-child {
  border-bottom: none;
}

.segment-color {
  width: 28px;
  height: 24px;
  padding: 1px;
  border: 1px solid var(--border);
  border-radius: 4px;
  cursor: pointer;
  flex-shrink: 0;
}

.segment-name {
  flex: 1;
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
  font-weight: 500;
  cursor: default;
}

.segment-name[title]:not(.is-other) {
  cursor: text;
}

.segment-name[title]:not(.is-other):hover {
  background: #f0f2f5;
  border-radius: 3px;
}

.segment-value {
  color: var(--text-secondary);
  flex-shrink: 0;
  font-size: 0.8rem;
  cursor: default;
}

.segment-value[title]:hover {
  background: #f0f2f5;
  border-radius: 3px;
}

.inline-edit-input {
  font-family: inherit;
  font-size: 0.85rem;
  padding: 2px 4px;
  border: 1px solid var(--primary);
  border-radius: 3px;
  outline: none;
  box-shadow: 0 0 0 2px rgba(74, 144, 217, 0.2);
  background: #fff;
  min-width: 0;
}

.inline-edit-input.name-edit {
  flex: 1;
  min-width: 60px;
}

.inline-edit-input.value-edit {
  width: 60px;
  flex-shrink: 0;
  text-align: right;
}

.search-card-btn {
  background: none;
  border: 1px solid var(--border);
  border-radius: 4px;
  padding: 2px 6px;
  cursor: pointer;
  font-size: 0.8rem;
  color: var(--primary);
  transition: all 0.2s;
  flex-shrink: 0;
}

.search-card-btn:hover {
  background: var(--primary);
  color: #fff;
}

.search-card-btn.has-image {
  background: #e8f5e9;
  border-color: #4caf50;
  color: #2e7d32;
}

/* === Canvas Panel === */
.canvas-panel {
  display: flex;
  flex-direction: column;
  align-items: center;
  padding: 24px;
  background: var(--bg);
  gap: 12px;
  min-height: 0;
}

.canvas-zoom-controls {
  display: flex;
  align-items: center;
  gap: 8px;
  flex-shrink: 0;
}

.btn-zoom {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  width: 32px;
  height: 32px;
  border: 1px solid var(--border);
  border-radius: 6px;
  background: #fff;
  font-size: 1.1rem;
  cursor: pointer;
  color: var(--text);
  transition: all 0.15s;
}

.btn-zoom:hover {
  background: #f0f2f5;
  border-color: #999;
}

#zoom-reset-btn {
  width: auto;
  padding: 0 10px;
  font-size: 0.8rem;
  font-weight: 500;
}

#zoom-level {
  font-size: 0.85rem;
  color: var(--text-secondary);
  min-width: 40px;
  text-align: center;
}

.canvas-scroll-area {
  flex: 1;
  overflow: auto;
  width: 100%;
  display: flex;
  align-items: center;
  justify-content: center;
  min-height: 0;
}

.canvas-wrapper {
  background: #fff;
  border-radius: var(--radius);
  box-shadow: var(--shadow);
  padding: 8px;
  display: inline-block;
}

#pie-canvas {
  display: block;
  width: 500px;
  height: 500px;
}

.export-controls {
  display: flex;
  gap: 12px;
  flex-shrink: 0;
}

/* === Modal === */
.modal {
  position: fixed;
  top: 0;
  left: 0;
  width: 100%;
  height: 100%;
  z-index: 1000;
  display: flex;
  align-items: center;
  justify-content: center;
}

.modal.hidden {
  display: none;
}

.modal-backdrop {
  position: absolute;
  top: 0;
  left: 0;
  width: 100%;
  height: 100%;
  background: rgba(0, 0, 0, 0.5);
}

.modal-content {
  position: relative;
  background: #fff;
  border-radius: 12px;
  width: 90%;
  max-width: 700px;
  max-height: 80vh;
  display: flex;
  flex-direction: column;
  box-shadow: 0 8px 32px rgba(0,0,0,0.2);
}

.modal-header {
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 16px 20px;
  border-bottom: 1px solid var(--border);
}

.modal-header h3 {
  font-size: 1.1rem;
}

.modal-close {
  background: none;
  border: none;
  font-size: 1.5rem;
  cursor: pointer;
  color: #999;
  padding: 4px 8px;
  line-height: 1;
}

.modal-close:hover {
  color: #333;
}

.modal-body {
  padding: 16px 20px;
  overflow-y: auto;
}

.modal-body input[type="text"] {
  margin-bottom: 12px;
}

#card-search-loading {
  text-align: center;
  color: var(--text-secondary);
  padding: 20px;
}

#card-search-results {
  display: grid;
  grid-template-columns: repeat(auto-fill, minmax(120px, 1fr));
  gap: 10px;
}

.card-thumb {
  cursor: pointer;
  border: 2px solid transparent;
  border-radius: 6px;
  overflow: hidden;
  transition: all 0.2s;
  aspect-ratio: 63/88;
  background: #f5f5f5;
}

.card-thumb:hover {
  border-color: var(--primary);
  transform: scale(1.03);
}

.card-thumb img {
  width: 100%;
  height: 100%;
  object-fit: cover;
  display: block;
}

.card-thumb-label {
  font-size: 0.7rem;
  text-align: center;
  padding: 2px 4px;
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
  background: rgba(0,0,0,0.6);
  color: #fff;
  margin-top: -24px;
  position: relative;
}

/* === Responsive === */
@media (max-width: 900px) {
  .app-container {
    grid-template-columns: 1fr;
  }

  .controls-panel {
    max-height: none;
    border-right: none;
    border-bottom: 1px solid var(--border);
  }

  .canvas-panel {
    padding: 16px;
  }
}
