/* ============================================================
   Textarea component styles
   Uses ui-theme-tokens.css variables for light/dark consistency
   ============================================================ */

/* ── Wrapper ──────────────────────────────────────────────── */
.usim-textarea-wrapper {
    display: flex;
    flex-direction: column;
    gap: 8px;
    font-family: var(--usim-font-family, sans-serif);
    width: 100%;
    box-sizing: border-box;
}

/* ── Label ────────────────────────────────────────────────── */
.usim-textarea-label-row {
    display: inline-flex;
    align-items: center;
    gap: 6px;
}

.usim-textarea-label {
    font-size: 14px;
    font-weight: 500;
    color: var(--ui-text-strong);
}

.usim-textarea-label.required::after {
    content: ' *';
    color: var(--usim-form-required-color, var(--usim-color-danger));
}

.usim-textarea-help-icon {
    display: inline-flex;
    align-items: center;
    justify-content: center;
    width: 16px;
    height: 16px;
    border-radius: 50%;
    border: 1px solid var(--usim-form-control-border, var(--ui-border));
    color: var(--ui-text-muted);
    font-size: 11px;
    font-weight: 700;
    cursor: help;
    user-select: none;
    position: relative;
    background: var(--ui-surface-muted);
}

.usim-textarea-help-icon::after {
    content: attr(data-tooltip);
    position: absolute;
    left: 50%;
    bottom: calc(100% + 8px);
    transform: translateX(-50%);
    min-width: 180px;
    max-width: 320px;
    width: max-content;
    padding: 8px 10px;
    border-radius: 6px;
    background: rgba(15, 23, 36, 0.95);
    color: #ffffff;
    font-size: 12px;
    font-weight: 500;
    line-height: 1.35;
    text-align: left;
    white-space: normal;
    box-shadow: 0 8px 24px rgba(0, 0, 0, 0.25);
    opacity: 0;
    pointer-events: none;
    transition: opacity 0.15s ease;
    z-index: 1000;
}

.usim-textarea-help-icon::before {
    content: '';
    position: absolute;
    left: 50%;
    bottom: calc(100% + 3px);
    transform: translateX(-50%);
    border: 5px solid transparent;
    border-top-color: rgba(15, 23, 36, 0.95);
    opacity: 0;
    pointer-events: none;
    transition: opacity 0.15s ease;
    z-index: 1001;
}

.usim-textarea-help-icon:hover::after,
.usim-textarea-help-icon:hover::before {
    opacity: 1;
}

/* ── Body ─────────────────────────────────────────────────── */
.usim-textarea-body {
    display: flex;
    flex-direction: column;
    border: 1px solid var(--usim-form-control-border, var(--ui-border));
    border-radius: 10px;
    overflow: hidden;
    background: var(--usim-form-control-bg, var(--ui-surface));
    transition: border-color 0.15s ease, box-shadow 0.15s ease;
}

.usim-textarea-body:focus-within {
    border-color: var(--usim-form-control-focus-border, var(--usim-color-primary));
    box-shadow: 0 0 0 3px var(--usim-form-control-focus-ring, rgba(52, 152, 219, 0.12));
}

.usim-textarea-wrapper.has-error .usim-textarea-body {
    border-color: var(--usim-error-color, #e53e3e);
}

/* ── Native textarea element ──────────────────────────────── */
.usim-textarea-el {
    width: 100%;
    min-height: 140px;
    padding: 12px 14px;
    font-size: 0.9rem;
    font-family: inherit;
    line-height: 1.55;
    color: var(--usim-text-primary);
    background: transparent;
    border: none;
    outline: none;
    resize: none;
    box-sizing: border-box;
    overflow-y: auto;
    overflow-x: hidden;
}

.usim-textarea-el-markdown {
    font-family: ui-monospace, SFMono-Regular, Menlo, Monaco, Consolas, "Liberation Mono", "Courier New", monospace;
    font-size: 0.88rem;
}

.usim-textarea-body.mode-plain {
    background: color-mix(in srgb, var(--usim-bg-secondary) 92%, var(--usim-bg-primary));
    width: 100%;
    overflow: hidden;
    border-radius: 10px;
}

.usim-textarea-body.mode-plain .usim-textarea-el {
    display: block;
    width: 100%;
    min-height: 150px;
    resize: none;
}

.usim-textarea-el::placeholder {
    color: var(--usim-text-muted, #9ca3af);
}

.usim-textarea-el:disabled {
    opacity: 0.5;
    cursor: not-allowed;
    resize: none;
}

.usim-textarea-el:read-only {
    background: var(--usim-input-readonly-bg, color-mix(in srgb, var(--usim-bg-secondary) 80%, var(--usim-bg-primary)));
    cursor: default;
}

/* ── Markdown toolbar ─────────────────────────────────────── */
.usim-textarea-toolbar {
    display: flex;
    flex-wrap: wrap;
    align-items: center;
    gap: 4px;
    padding: 8px 10px;
    background: var(--usim-toolbar-bg, color-mix(in srgb, var(--usim-bg-primary) 60%, var(--usim-bg-secondary)));
    border-bottom: 1px solid var(--usim-border-color);
}

.usim-toolbar-btn {
    padding: 3px 8px;
    font-size: 0.76rem;
    font-family: inherit;
    font-weight: 600;
    color: var(--usim-text-secondary);
    background: transparent;
    border: 1px solid color-mix(in srgb, var(--usim-border-color) 80%, transparent);
    border-radius: 4px;
    cursor: pointer;
    line-height: 1.4;
    transition: background 0.1s, color 0.1s, border-color 0.1s;
    white-space: nowrap;
}

.usim-toolbar-btn:hover {
    background: var(--usim-btn-hover-bg, color-mix(in srgb, var(--usim-accent, var(--usim-primary)) 12%, transparent));
    border-color: var(--usim-accent, var(--usim-primary));
    color: var(--usim-text-primary);
}

.usim-toolbar-sep {
    width: 1px;
    height: 18px;
    background: var(--usim-border-color);
    margin: 0 4px;
    flex-shrink: 0;
}

/* ── Mobile tabs ──────────────────────────────────────────── */
.usim-textarea-tabs {
    display: none;  /* hidden on desktop */
    border-bottom: 1px solid var(--usim-border-color);
}

.usim-textarea-tab {
    padding: 5px 14px;
    font-size: 0.8rem;
    font-weight: 500;
    cursor: pointer;
    background: transparent;
    border: none;
    color: var(--usim-text-muted);
    border-bottom: 2px solid transparent;
    transition: color 0.15s, border-color 0.15s;
}

.usim-textarea-tab.active {
    color: var(--usim-accent, var(--usim-primary));
    border-bottom-color: var(--usim-accent, var(--usim-primary));
}

.tab-hidden {
    display: none !important;
}

/* ── Split view ───────────────────────────────────────────── */
.usim-textarea-split {
    display: grid;
    grid-template-columns: minmax(0, 1fr) minmax(0, 1fr);
    min-height: 240px;
    overflow: hidden;
}

.usim-textarea-editor-pane {
    display: flex;
    flex-direction: column;
    border-right: 1px solid var(--usim-border-color);
    min-width: 0;
    min-height: 0;
}

.usim-textarea-editor-pane .usim-textarea-el {
    flex: 1;
    resize: none;  /* disable resize in split view */
    min-height: 0;
    height: 100%;
}

.usim-textarea-preview-pane {
    display: flex;
    flex-direction: column;
    min-width: 0;
    min-height: 0;
    overflow-y: auto;
    padding: 0;
    background: var(--usim-preview-bg, var(--usim-bg-primary));
}

.usim-textarea-pane-title {
    font-size: 0.72rem;
    letter-spacing: 0.06em;
    text-transform: uppercase;
    font-weight: 700;
    color: var(--usim-text-muted);
    padding: 7px 12px;
    border-bottom: 1px solid var(--usim-border-color);
    background: color-mix(in srgb, var(--usim-bg-primary) 72%, var(--usim-bg-secondary));
}

.usim-textarea-preview-content {
    padding: 12px 14px;
}

/* ── Markdown preview ─────────────────────────────────────── */
.usim-md-preview {
    font-size: 0.9rem;
    line-height: 1.7;
    color: var(--usim-text-primary);
    word-break: break-word;
}

.usim-md-preview h1,
.usim-md-preview h2,
.usim-md-preview h3,
.usim-md-preview h4,
.usim-md-preview h5,
.usim-md-preview h6 {
    margin: 0.75em 0 0.4em;
    font-weight: 700;
    line-height: 1.3;
    color: var(--usim-text-primary);
}

.usim-md-preview h1 { font-size: 1.5rem; }
.usim-md-preview h2 { font-size: 1.25rem; }
.usim-md-preview h3 { font-size: 1.05rem; }

.usim-md-preview p {
    margin: 0.5em 0;
}

.usim-md-preview ul,
.usim-md-preview ol {
    margin: 0.4em 0;
    padding-left: 1.4em;
}

.usim-md-preview li {
    margin: 0.2em 0;
}

.usim-md-preview strong { font-weight: 700; }
.usim-md-preview em     { font-style: italic; }
.usim-md-preview del    { text-decoration: line-through; opacity: 0.7; }

.usim-md-preview code {
    font-family: monospace;
    font-size: 0.85em;
    background: var(--usim-code-bg, color-mix(in srgb, var(--usim-bg-secondary) 70%, var(--usim-accent, #6366f1) 30%));
    color: var(--usim-code-color, var(--usim-text-primary));
    padding: 1px 5px;
    border-radius: 3px;
}

.usim-md-preview pre {
    background: var(--usim-code-block-bg, color-mix(in srgb, var(--usim-bg-secondary) 85%, #000));
    border-radius: 6px;
    padding: 10px 14px;
    overflow-x: auto;
    margin: 0.6em 0;
}

.usim-md-preview pre code {
    background: transparent;
    padding: 0;
    font-size: 0.83em;
    color: var(--usim-code-block-color, var(--usim-text-primary));
}

.usim-md-preview blockquote {
    border-left: 3px solid var(--usim-accent, var(--usim-primary));
    margin: 0.5em 0;
    padding: 4px 12px;
    color: var(--usim-text-secondary);
    background: color-mix(in srgb, var(--usim-accent, var(--usim-primary)) 6%, transparent);
    border-radius: 0 4px 4px 0;
}

.usim-md-preview hr {
    border: none;
    border-top: 1px solid var(--usim-border-color);
    margin: 1em 0;
}

.usim-md-preview a {
    color: var(--usim-accent, var(--usim-primary));
    text-decoration: underline;
}

/* ── Footer ───────────────────────────────────────────────── */
.usim-textarea-footer {
    display: flex;
    align-items: center;
    justify-content: space-between;
    gap: 10px;
    min-height: 18px;
    flex-wrap: wrap;
    padding: 0 2px;
}

.usim-textarea-counter {
    font-size: 0.75rem;
    color: var(--usim-text-muted);
    margin-left: auto;
    transition: color 0.15s;
}

.usim-textarea-counter.near-limit {
    color: var(--usim-warning-color, #d97706);
}

.usim-textarea-counter.at-limit {
    color: var(--usim-error-color, #e53e3e);
    font-weight: 600;
}

.usim-textarea-error-msg {
    font-size: 0.78rem;
    color: var(--usim-error-color, #e53e3e);
    flex: 1;
}

.usim-textarea-help {
    font-size: 0.78rem;
    color: var(--usim-text-muted);
    flex: 1;
}

/* ── Responsive: single-column on narrow screens ──────────── */
@media (max-width: 600px) {
    .usim-textarea-split {
        grid-template-columns: 1fr;
    }

    .usim-textarea-editor-pane {
        border-right: none;
        border-bottom: 1px solid var(--usim-border-color);
    }

    .usim-textarea-tabs {
        display: flex;
    }

    .usim-textarea-split {
        min-height: 220px;
        height: auto !important;
    }

    /* In mobile, tab switching handles visibility */
    .usim-textarea-split > .usim-textarea-preview-pane {
        /* visible by default until JS hides it */
    }
}
