/* ===== GLOBAL RESET & BASE STYLES ===== */
*, *::before, *::after {
  box-sizing: border-box;
}

:root {
  /* Light Theme Colors (Clean, textboard-like) */
  --bg-app: #fbfbfb;
  --bg-surface: #ffffff;
  --bg-surface-secondary: #f4f4f5;
  --bg-surface-hover: #ececee;
  --bg-surface-active: #e4e4e7;
  
  --text-main: #18181b;
  --text-secondary: #71717a;
  --text-tertiary: #a1a1aa;
  --text-inverse: #ffffff;

  --border-light: rgba(0, 0, 0, 0.08);
  --border-medium: rgba(0, 0, 0, 0.14);
  --border-focus: #2563eb;
  --focus-ring: rgba(37, 99, 235, 0.18);

  --cell-bg: #ffffff;
  --cell-bg-hover: #fafafa;
  --cell-bg-active: #ffffff;
  --header-bg: #f8f8f9;
  --header-text: #888890;
  --header-active-bg: #eaeaea;
  --header-active-text: #18181b;

  --accent-primary: #18181b;
  --accent-primary-hover: #27272a;
  --accent-blue: #2563eb;
  --accent-blue-hover: #1d4ed8;

  --shadow-sm: 0 1px 2px rgba(0, 0, 0, 0.04);
  --shadow-md: 0 4px 12px rgba(0, 0, 0, 0.08);
  --shadow-lg: 0 12px 32px rgba(0, 0, 0, 0.12);

  --radius-sm: 4px;
  --radius-md: 6px;
  --radius-lg: 10px;

  --font-sans: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, "Helvetica Neue", Arial, sans-serif;
  --font-mono: ui-monospace, SFMono-Regular, Menlo, Monaco, Consolas, monospace;

  --toolbar-height: 40px;
  --statusbar-height: 28px;
  --default-cell-width: 240px;
  --default-cell-min-height: 80px;
}

body.dark-theme {
  --bg-app: #121214;
  --bg-surface: #18181b;
  --bg-surface-secondary: #27272a;
  --bg-surface-hover: #323238;
  --bg-surface-active: #3f3f46;

  --text-main: #f4f4f5;
  --text-secondary: #a1a1aa;
  --text-tertiary: #71717a;
  --text-inverse: #18181b;

  --border-light: rgba(255, 255, 255, 0.08);
  --border-medium: rgba(255, 255, 255, 0.15);
  --border-focus: #3b82f6;
  --focus-ring: rgba(59, 130, 246, 0.25);

  --cell-bg: #1c1c20;
  --cell-bg-hover: #222227;
  --cell-bg-active: #222227;
  --header-bg: #161619;
  --header-text: #71717a;
  --header-active-bg: #27272a;
  --header-active-text: #f4f4f5;

  --accent-primary: #f4f4f5;
  --accent-primary-hover: #e4e4e7;
  --shadow-sm: 0 1px 2px rgba(0, 0, 0, 0.4);
  --shadow-md: 0 4px 12px rgba(0, 0, 0, 0.5);
  --shadow-lg: 0 12px 32px rgba(0, 0, 0, 0.7);
}

html, body {
  margin: 0;
  padding: 0;
  width: 100vw;
  height: 100vh;
  overflow: hidden;
  background-color: var(--bg-app);
  color: var(--text-main);
  font-family: var(--font-sans);
  font-size: 12px;
  line-height: 1.5;
  -webkit-font-smoothing: antialiased;
  -moz-osx-font-smoothing: grayscale;
}

/* ===== UTILITIES ===== */
.hidden { display: none !important; }
.font-mono { font-family: var(--font-mono); }

kbd {
  display: inline-block;
  padding: 1px 4px;
  font-family: var(--font-mono);
  font-size: 10px;
  line-height: 1.2;
  color: var(--text-secondary);
  background-color: var(--bg-surface-secondary);
  border: 1px solid var(--border-medium);
  border-radius: 3px;
  box-shadow: 0 1px 0 rgba(0,0,0,0.08);
}

button {
  font-family: inherit;
  font-size: inherit;
  border: none;
  background: none;
  cursor: pointer;
  outline: none;
}

/* ===== APP LAYOUT ===== */
.app-container {
  display: flex;
  flex-direction: column;
  width: 100vw;
  height: 100vh;
  overflow: hidden;
  position: relative;
}

/* ===== TOOLBAR ===== */
.toolbar {
  height: var(--toolbar-height);
  background: var(--bg-surface);
  border-bottom: 1px solid var(--border-light);
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 0 12px;
  gap: 12px;
  z-index: 50;
  flex-shrink: 0;
  backdrop-filter: blur(10px);
}

.toolbar-left, .toolbar-right {
  display: flex;
  align-items: center;
  gap: 6px;
}

.toolbar-center {
  flex: 1;
  display: flex;
  justify-content: center;
}

.title-container {
  display: flex;
  align-items: center;
  gap: 6px;
}

.memo-title-input {
  font-size: 13px;
  font-weight: 600;
  color: var(--text-main);
  background: transparent;
  border: 1px solid transparent;
  border-radius: var(--radius-sm);
  padding: 3px 6px;
  min-width: 140px;
  max-width: 280px;
  outline: none;
  transition: border-color 0.15s, background-color 0.15s;
}

.memo-title-input:hover {
  background-color: var(--bg-surface-secondary);
}

.memo-title-input:focus {
  background-color: var(--bg-surface);
  border-color: var(--border-focus);
  box-shadow: 0 0 0 2px var(--focus-ring);
}

.toolbar-btn {
  display: inline-flex;
  align-items: center;
  gap: 4px;
  padding: 4px 8px;
  font-size: 11px;
  font-weight: 500;
  color: var(--text-secondary);
  background-color: transparent;
  border: 1px solid var(--border-light);
  border-radius: var(--radius-sm);
  transition: all 0.14s ease;
  white-space: nowrap;
}

.toolbar-btn:hover {
  background-color: var(--bg-surface-hover);
  color: var(--text-main);
  border-color: var(--border-medium);
}

.toolbar-btn:active {
  background-color: var(--bg-surface-active);
}

.icon-btn {
  padding: 4px 6px;
}

.toolbar-divider {
  width: 1px;
  height: 16px;
  background-color: var(--border-light);
  margin: 0 2px;
}

/* ===== SEGMENTED CONTROL ===== */
.view-segmented-control {
  display: flex;
  background: var(--bg-surface-secondary);
  border: 1px solid var(--border-light);
  border-radius: 6px;
  padding: 2px;
  gap: 2px;
}

.seg-btn {
  display: inline-flex;
  align-items: center;
  gap: 4px;
  padding: 3px 10px;
  font-size: 11px;
  font-weight: 500;
  color: var(--text-secondary);
  border-radius: 4px;
  transition: all 0.15s ease;
}

.seg-btn:hover {
  color: var(--text-main);
}

.seg-btn.active {
  background: var(--bg-surface);
  color: var(--text-main);
  box-shadow: var(--shadow-sm);
  font-weight: 600;
}

/* ===== MAIN MATRIX VIEW ===== */
.view-content {
  flex: 1;
  overflow: hidden;
  position: relative;
  display: flex;
  flex-direction: column;
}

.matrix-scroll-wrapper {
  flex: 1;
  overflow: auto;
  padding: 36px 40px 80px 40px;
  background-color: var(--bg-app);
  position: relative;
  display: flex;
  justify-content: center;
  align-items: flex-start;
}

.matrix-canvas {
  display: flex;
  flex-direction: column;
  align-items: center;
  margin: auto;
  max-width: 100%;
}

/* ===== MATRIX GRID COMPONENT ===== */
.matrix-grid-root {
  display: inline-block;
  background: var(--bg-surface);
  border: 1px solid var(--border-medium);
  border-radius: var(--radius-md);
  box-shadow: var(--shadow-sm);
  overflow: visible;
  position: relative;
}

.matrix-table {
  border-collapse: collapse;
  table-layout: fixed;
  width: max-content;
}

/* Column Resize Handle (on top-row cells) */
.col-resize-handle {
  position: absolute;
  top: 0;
  right: -3px;
  width: 6px;
  height: 100%;
  cursor: col-resize;
  z-index: 10;
  transition: background-color 0.15s;
}
.col-resize-handle:hover,
.col-resize-handle.resizing {
  background-color: var(--accent-blue);
}

/* Grid Memo Cell (Horizontal Rectangle) */
.matrix-cell-td {
  border-right: 1px solid var(--border-light);
  border-bottom: 1px solid var(--border-light);
  padding: 0;
  vertical-align: middle;
  height: var(--default-cell-min-height);
  background-color: var(--cell-bg);
  transition: background-color 0.12s;
  position: relative;
  cursor: text;
}

/* 1列目の右側ライン（区切りを少し太めに強調） */
.matrix-cell-td[data-col="0"] {
  border-right: 2px solid var(--border-medium);
}

.matrix-cell-td:last-child {
  border-right: none;
}

.matrix-row:last-child .matrix-cell-td {
  border-bottom: none;
}

/* 最上段（キー/見出し行: 縦サイズ約1/3 & ほんのりトーン変更） */
.matrix-row[data-row="0"] .matrix-cell-td {
  background-color: var(--bg-surface-secondary);
  height: 34px;
}

.matrix-cell-td:hover {
  background-color: var(--cell-bg-hover);
}

.matrix-row[data-row="0"] .matrix-cell-td:hover {
  background-color: var(--bg-surface-hover);
}

.matrix-cell-content {
  display: block;
  width: 100%;
  padding: 8px 14px;
  font-size: 12px;
  line-height: 1.6;
  text-align: left;
  color: var(--text-main);
  background: transparent;
  border: none;
  outline: none;
  resize: none;
  overflow-y: hidden;
  white-space: pre-wrap;
  word-break: break-word;
  font-family: inherit;
  box-sizing: border-box;
  transition: box-shadow 0.15s, background-color 0.15s;
}

/* 最上段のセル内テキストエリア（コンパクト） */
.matrix-row[data-row="0"] .matrix-cell-content {
  padding: 5px 14px;
  font-weight: 600;
  font-size: 11.5px;
}

/* Cell Focus Highlight */
.matrix-cell-td.cell-focused {
  background-color: var(--cell-bg-active);
  z-index: 2;
}

.matrix-cell-td.cell-focused::after {
  content: '';
  position: absolute;
  top: 0;
  left: 0;
  right: 0;
  bottom: 0;
  border: 2px solid var(--border-focus);
  border-radius: 2px;
  pointer-events: none;
  box-shadow: 0 0 0 2px var(--focus-ring);
  z-index: 5;
}

/* FLOATING ADD BUTTONS */
.floating-actions {
  position: absolute;
  bottom: 40px;
  right: 32px;
  display: flex;
  gap: 8px;
  z-index: 20;
}

.floating-btn {
  background: var(--bg-surface);
  border: 1px solid var(--border-medium);
  color: var(--text-main);
  padding: 6px 12px;
  font-size: 11px;
  font-weight: 500;
  border-radius: 20px;
  box-shadow: var(--shadow-md);
  display: flex;
  align-items: center;
  gap: 4px;
  transition: all 0.15s ease;
}

.floating-btn:hover {
  background-color: var(--bg-surface-hover);
  transform: translateY(-1px);
  box-shadow: var(--shadow-lg);
}

/* ===== STATUS BAR ===== */
.statusbar {
  height: var(--statusbar-height);
  background: var(--bg-surface);
  border-top: 1px solid var(--border-light);
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 0 12px;
  font-size: 10px;
  color: var(--text-secondary);
  flex-shrink: 0;
  user-select: none;
}

.statusbar-left, .statusbar-right {
  display: flex;
  align-items: center;
  gap: 8px;
}

.status-divider {
  color: var(--text-tertiary);
  font-size: 8px;
}

.status-hint {
  color: var(--text-tertiary);
  font-size: 10px;
}

.status-save-indicator {
  color: #10b981;
  font-weight: 500;
}

.status-save-indicator.saving {
  color: #f59e0b;
}

/* ===== CARDS VIEW ===== */
.cards-view {
  background-color: var(--bg-app);
  padding: 32px 40px;
  overflow-y: auto;
}

.cards-header {
  display: flex;
  align-items: center;
  justify-content: space-between;
  margin-bottom: 24px;
  flex-wrap: wrap;
  gap: 16px;
}

.cards-header-title h2 {
  margin: 0 0 4px 0;
  font-size: 18px;
  font-weight: 600;
  color: var(--text-main);
}

.cards-header-title p {
  margin: 0;
  font-size: 12px;
  color: var(--text-secondary);
}

.cards-header-actions {
  display: flex;
  align-items: center;
  gap: 10px;
}

.cards-search-input, .sidebar-search input {
  padding: 6px 12px;
  font-size: 11px;
  background: var(--bg-surface);
  border: 1px solid var(--border-medium);
  border-radius: var(--radius-sm);
  color: var(--text-main);
  outline: none;
  min-width: 220px;
}

.cards-search-input:focus, .sidebar-search input:focus {
  border-color: var(--border-focus);
  box-shadow: 0 0 0 2px var(--focus-ring);
}

.primary-btn {
  background: var(--accent-primary);
  color: var(--text-inverse);
  padding: 6px 14px;
  font-size: 11px;
  font-weight: 500;
  border-radius: var(--radius-sm);
  display: inline-flex;
  align-items: center;
  gap: 6px;
  transition: all 0.15s ease;
}

.primary-btn:hover {
  opacity: 0.9;
  transform: translateY(-0.5px);
}

.primary-btn kbd {
  background: rgba(255,255,255,0.2);
  border-color: rgba(255,255,255,0.3);
  color: inherit;
}

.cards-grid {
  display: grid;
  grid-template-columns: repeat(auto-fill, minmax(280px, 1fr));
  gap: 16px;
}

.memo-card {
  background: var(--bg-surface);
  border: 1px solid var(--border-light);
  border-radius: var(--radius-md);
  padding: 16px;
  cursor: pointer;
  transition: all 0.16s ease;
  display: flex;
  flex-direction: column;
  gap: 10px;
  box-shadow: var(--shadow-sm);
  position: relative;
}

.memo-card:hover {
  border-color: var(--border-medium);
  transform: translateY(-2px);
  box-shadow: var(--shadow-md);
}

.memo-card.card-active {
  border-color: var(--border-focus);
  box-shadow: 0 0 0 1px var(--border-focus);
}

.memo-card-header {
  display: flex;
  align-items: center;
  justify-content: space-between;
}

.memo-card-title {
  font-size: 13px;
  font-weight: 600;
  color: var(--text-main);
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}

.memo-card-badge {
  font-size: 9px;
  font-family: var(--font-mono);
  background: var(--bg-surface-secondary);
  color: var(--text-secondary);
  padding: 2px 6px;
  border-radius: 3px;
}

.memo-card-preview {
  display: grid;
  gap: 3px;
  background: var(--bg-surface-secondary);
  border-radius: 4px;
  padding: 6px;
  max-height: 110px;
  overflow: hidden;
  font-size: 9px;
  line-height: 1.3;
}

.memo-card-cell-preview {
  background: var(--cell-bg);
  padding: 4px 6px;
  border-radius: 2px;
  color: var(--text-secondary);
  overflow: hidden;
  white-space: nowrap;
  text-overflow: ellipsis;
}

.memo-card-footer {
  display: flex;
  align-items: center;
  justify-content: space-between;
  font-size: 10px;
  color: var(--text-tertiary);
}

.memo-card-delete-btn {
  opacity: 0;
  padding: 2px 6px;
  border-radius: 3px;
  color: #ef4444;
  transition: opacity 0.15s, background-color 0.15s;
}

.memo-card:hover .memo-card-delete-btn {
  opacity: 1;
}

.memo-card-delete-btn:hover {
  background-color: rgba(239, 68, 68, 0.1);
}

/* ===== SIDEBAR ===== */
.sidebar {
  position: fixed;
  top: 0;
  left: 0;
  bottom: 0;
  width: 280px;
  background: var(--bg-surface);
  border-right: 1px solid var(--border-medium);
  box-shadow: var(--shadow-lg);
  z-index: 100;
  display: flex;
  flex-direction: column;
  transform: translateX(-100%);
  transition: transform 0.22s cubic-bezier(0.16, 1, 0.3, 1);
}

.sidebar.open {
  transform: translateX(0);
}

.sidebar-backdrop {
  position: fixed;
  top: 0; left: 0; right: 0; bottom: 0;
  background: rgba(0, 0, 0, 0.3);
  backdrop-filter: blur(2px);
  z-index: 90;
  transition: opacity 0.2s ease;
}

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

.sidebar-title {
  display: flex;
  align-items: center;
  gap: 8px;
  font-size: 13px;
  font-weight: 700;
  color: var(--text-main);
}

.sidebar-actions {
  padding: 12px 16px 8px 16px;
}

.sidebar-actions .primary-btn {
  width: 100%;
  justify-content: center;
}

.sidebar-search {
  padding: 4px 16px 12px 16px;
}

.sidebar-search input {
  width: 100%;
}

.sidebar-list-container {
  flex: 1;
  overflow-y: auto;
  padding: 0 8px;
}

.sidebar-section-title {
  padding: 6px 8px;
  font-size: 10px;
  font-weight: 600;
  text-transform: uppercase;
  letter-spacing: 0.5px;
  color: var(--text-tertiary);
}

.sidebar-memo-item {
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 8px 10px;
  border-radius: var(--radius-sm);
  color: var(--text-main);
  cursor: pointer;
  margin-bottom: 2px;
  transition: background-color 0.12s;
}

.sidebar-memo-item:hover {
  background-color: var(--bg-surface-hover);
}

.sidebar-memo-item.active {
  background-color: var(--bg-surface-active);
  font-weight: 600;
}

.sidebar-memo-item-title {
  font-size: 11px;
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
  flex: 1;
}

.sidebar-memo-item-size {
  font-size: 9px;
  font-family: var(--font-mono);
  color: var(--text-tertiary);
  margin-left: 6px;
}

.sidebar-footer {
  padding: 12px 16px;
  border-top: 1px solid var(--border-light);
  display: flex;
  flex-direction: column;
  gap: 4px;
}

.sidebar-footer-btn {
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 6px 8px;
  font-size: 11px;
  color: var(--text-secondary);
  border-radius: var(--radius-sm);
  transition: background-color 0.14s;
}

.sidebar-footer-btn:hover {
  background-color: var(--bg-surface-hover);
  color: var(--text-main);
}

/* ===== MODAL DIALOGS ===== */
.modal-overlay {
  position: fixed;
  top: 0; left: 0; right: 0; bottom: 0;
  background: rgba(0, 0, 0, 0.45);
  backdrop-filter: blur(4px);
  z-index: 200;
  display: flex;
  align-items: center;
  justify-content: center;
  padding: 20px;
  animation: fadeIn 0.15s ease;
}

.modal-card {
  background: var(--bg-surface);
  border: 1px solid var(--border-medium);
  border-radius: var(--radius-lg);
  width: 100%;
  max-width: 620px;
  max-height: 85vh;
  box-shadow: var(--shadow-lg);
  display: flex;
  flex-direction: column;
  overflow: hidden;
  animation: scaleIn 0.18s cubic-bezier(0.16, 1, 0.3, 1);
}

.help-modal-card {
  max-width: 580px;
}

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

.modal-title {
  font-size: 14px;
  font-weight: 600;
  color: var(--text-main);
}

.modal-body {
  padding: 18px;
  overflow-y: auto;
  display: flex;
  flex-direction: column;
  gap: 16px;
}

.modal-desc {
  margin: 0;
  font-size: 12px;
  color: var(--text-secondary);
}

/* Export Options */
.export-options-grid {
  display: grid;
  grid-template-columns: repeat(2, 1fr);
  gap: 10px;
}

.export-card {
  background: var(--bg-surface-secondary);
  border: 1px solid var(--border-light);
  border-radius: var(--radius-md);
  padding: 12px;
  display: flex;
  flex-direction: column;
  gap: 6px;
}

.export-card-header {
  display: flex;
  align-items: center;
  gap: 6px;
  font-size: 12px;
  font-weight: 600;
}

.export-info {
  margin: 0;
  font-size: 10px;
  color: var(--text-secondary);
  flex: 1;
}

.export-card-actions {
  display: flex;
  gap: 6px;
  margin-top: 4px;
}

.export-card-actions button {
  flex: 1;
  padding: 4px 8px;
  font-size: 10px;
  font-weight: 500;
  background: var(--bg-surface);
  border: 1px solid var(--border-medium);
  border-radius: var(--radius-sm);
  color: var(--text-main);
  transition: all 0.12s;
}

.export-card-actions button:hover {
  background-color: var(--accent-primary);
  color: var(--text-inverse);
  border-color: var(--accent-primary);
}

.export-preview-container {
  background: var(--bg-surface-secondary);
  border: 1px solid var(--border-light);
  border-radius: var(--radius-sm);
  padding: 10px;
}

.preview-header {
  display: flex;
  justify-content: space-between;
  font-size: 10px;
  font-weight: 600;
  color: var(--text-tertiary);
  margin-bottom: 6px;
}

.export-preview-content {
  margin: 0;
  font-family: var(--font-mono);
  font-size: 11px;
  line-height: 1.4;
  white-space: pre;
  overflow-x: auto;
  max-height: 140px;
  color: var(--text-main);
}

/* Shortcut Help Table */
.shortcut-groups {
  display: flex;
  flex-direction: column;
  gap: 16px;
}

.shortcut-group-title {
  font-size: 11px;
  font-weight: 700;
  color: var(--text-secondary);
  margin-bottom: 6px;
  border-bottom: 1px solid var(--border-light);
  padding-bottom: 4px;
}

.shortcut-row {
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 4px 0;
  gap: 12px;
}

.shortcut-keys {
  display: flex;
  gap: 4px;
  align-items: center;
  white-space: nowrap;
}

.shortcut-desc {
  font-size: 11px;
  color: var(--text-main);
  text-align: right;
}

/* ===== TOAST NOTIFICATION ===== */
.toast {
  position: fixed;
  bottom: 38px;
  left: 50%;
  transform: translateX(-50%);
  background: var(--accent-primary);
  color: var(--text-inverse);
  padding: 6px 16px;
  font-size: 11px;
  font-weight: 500;
  border-radius: 20px;
  box-shadow: var(--shadow-lg);
  z-index: 300;
  animation: slideUpFade 0.2s cubic-bezier(0.16, 1, 0.3, 1);
  pointer-events: none;
}

/* ===== KEYFRAME ANIMATIONS ===== */
@keyframes fadeIn {
  from { opacity: 0; }
  to { opacity: 1; }
}

@keyframes scaleIn {
  from { opacity: 0; transform: scale(0.96); }
  to { opacity: 1; transform: scale(1); }
}

@keyframes slideUpFade {
  from { opacity: 0; transform: translate(-50%, 8px); }
  to { opacity: 1; transform: translate(-50%, 0); }
}
